标签:
思路很好像,卡了我很久的就是当最大房间一样的时候判断输出哪个的条件, = = 简直无情
/* ID: 18906421 LANG: C++ PROG: castle */ #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int maxn = 55; int mat[maxn][maxn][2] = {0}; // 0 1 下 右 int n,m,vis[maxn][maxn]; int max1 = 0,max2 = 0,cnt = 0; void debug(){ for(int i = 0; i < n; i++){ for(int j = 0; j < m; j++) printf("[%d,%d],(%d %d)",i,j,mat[i][j][0],mat[i][j][1]); puts(""); } } void change(int x,int y,int v){ if(v == 8 || v == 9 || v == 10 || v == 12 || v == 11 || v == 13 || v == 14 || v == 15) mat[x][y][0] = 1; if(v == 4 || v == 5 || v == 6 || v == 12 || v == 7 || v == 14 || v == 13 || v == 15) mat[x][y][1] = 1; } int dfs(int x,int y){ vis[x][y] = 1; int ans = 1; if(!mat[x][y][0] && x + 1 < n && !vis[x + 1][y]) ans += dfs(x + 1,y); // 下 if(!mat[x][y][1] && y + 1 < m && !vis[x][y + 1]) ans += dfs(x,y + 1); // 右 if(x - 1 >= 0 && !mat[x - 1][y][0] && !vis[x - 1][y]) ans += dfs(x - 1,y); // 上 if(y - 1 >= 0 && !mat[x][y - 1][1] && !vis[x][y - 1]) ans += dfs(x,y - 1); // 左 return ans; } void solve(int &max_size,int &cc){ memset(vis,0,sizeof(vis)); for(int i = 0; i < n; i++) for(int j = 0; j < m; j++) if(!vis[i][j]){ max_size = max(max_size,dfs(i,j)); cc ++; } } int main(){ freopen("castle.in","r",stdin); freopen("castle.out","w",stdout); scanf("%d%d",&m,&n); for(int i = 0; i < n; i++) for(int j = 0; j < m; j++){ int x; scanf("%d",&x); change(i,j,x); } //debug(); solve(max1,cnt); //获得最大房间 printf("%d\n%d\n",cnt,max1); int dirr,pos_x,pos_y,max_size = max1,cc; for(int j = m - 1; j >= 0; j--) for(int i = 0; i < n; i++){ for(int k = 1; k >= 0; k --) if(mat[i][j][k]){ //k 为 0 破坏下面 1 破坏右边 max2 = 0;cc = 0; mat[i][j][k] = 0; solve(max2,cc); mat[i][j][k] = 1; int xx = k == 0 ? i + 1 : i; int yy = j; if(max2 > max_size){ pos_x = xx; pos_y = yy; max_size = max2; dirr = k; } else if(max2 == max_size){ if(yy < pos_y || (yy == pos_y && xx > pos_x)){ pos_x = xx; pos_y = yy; max_size = max2; dirr = k; } } } } if(dirr == 1) printf("%d\n%d %d %c\n",max_size,pos_x + 1,pos_y + 1,'E'); else printf("%d\n%d %d %c\n",max_size,pos_x + 1,pos_y + 1,'N'); return 0; }
标签:
原文地址:http://blog.csdn.net/u013451221/article/details/45062403