标签:
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5335
1 #include<stdio.h> 2 #include<cstring> 3 #include<cmath> 4 #include<algorithm> 5 using namespace std; 6 const int MAXN = 1010; 7 char R[MAXN][MAXN]; 8 bool used[MAXN][MAXN]; 9 int xx[1100000], yy[1100000]; 10 int my[4] = { 0, 0, 1, -1}; 11 int mx[4] = { 1, -1, 0, 0}; 12 int n,m; 13 void bfs(){ 14 memset(used,false,sizeof(used)); 15 used[1][1] = true; 16 int p = 1, h = 1; 17 xx[p] = 1; 18 yy[p] = 1; 19 for(; p <= h; ++p) 20 if(R[xx[p]][yy[p]] == ‘0‘){ 21 for( int i = 0; i < 4; ++i){ 22 int X = xx[p]+mx[i]; 23 int Y = yy[p]+my[i]; 24 if( 0 < X && X <= n && 0 < Y && Y <= m && !used[X][Y]){ 25 h++; 26 xx[h] = X; 27 yy[h] = Y; 28 used[X][Y] = true; 29 } 30 } 31 }//注意区分XY 32 if( R[n][m] == ‘0‘ && used[n][m]){ 33 printf("0\n"); 34 return; 35 } 36 int ma = 0; 37 for( int i = 1; i <= n; ++i) 38 for ( int j = 1; j <= m; ++j) 39 if(used[i][j]){ 40 ma = max(ma,i+j); 41 } 42 printf("1"); 43 for( int i = ma; i < n+m; ++i){ 44 char mi = ‘1‘; 45 for( int j = 1; j <= n; ++j ) 46 if( 1 <= i-j && i-j <= m && used[j][i-j]){ 47 mi = min(mi,R[j+1][i-j]); 48 mi = min(mi,R[j][i-j+1]); 49 } 50 printf("%c",mi); 51 for(int j = 1; j <= n; ++j ) 52 if( 1 <= i-j && i-j <= m && used[j][i-j]){ 53 if(R[j+1][i-j] == mi) 54 used[j+1][i-j] = true; 55 if(R[j][i-j+1] == mi) 56 used[j][i-j+1] = true; 57 } 58 } 59 printf("\n"); 60 } 61 int main() 62 { 63 freopen("../Ъ§Он/1009/1009.in", "r", stdin); 64 freopen("a.out", "w", stdout); 65 int T; 66 scanf("%d",&T); 67 while(T--){ 68 scanf("%d%d",&n,&m); 69 for( int i = 1; i <= n; ++i ) 70 scanf("%s",R[i]+1); 71 for (int i = 0; i <= n+1; ++i){ 72 R[i][0] = R[i][m+1] = ‘2‘; 73 } 74 for ( int i = 0; i <= m+1; ++i){ 75 R[0][i] = R[n+1][i] = ‘2‘; 76 } 77 bfs(); 78 // for ( int i = 0; i <= n +1; i++){ 79 // for( int j = 0; j <= m+1; j++) 80 // printf("%c",R[i][j]); 81 // printf("\n"); 82 // } 83 } 84 return 0; 85 }
标签:
原文地址:http://www.cnblogs.com/blueprintf/p/4703560.html