poj1110-double vision-搜索
题目:http://poj.org/problem?id=1110
其实思路挺简单,不过题意确实不够明了,而且从网页上都看不出来题目中那几个数字- -行间距太大了,粘下来放到文本里就能看出是01234
对每个symbol遍历各种可能的情况,从上到下,每行从左到右,看看某位置(或某两个位置)其他symbol是不是'o',如果存在其他symbol相应位置上是'o',就说明这个不是unique的,不符合要求,继续找,找不到就是impossible
这个代码有点罗嗦,抽空看看精简一下,有个题解似乎结构更好一点,在这里http://shaidaima.com/source/view/10279
//double vision搜索 #include <cstdio> char a[10][81]; int n, r, c;//n--num of symbols, r/c--num of rows/cols in each grid int upper_bound; bool search(int x){ int begin = x * (c + 1); int end = begin + c - 1;//inclusive for(int i = 0; i < r; i++){ for(int j = begin; j <= end; j++){ if(a[i][j] == 'o'){ int fwd = j, bwd = j;//forward,backward //bool pass = false; while((bwd -= (c + 1)) >= 0){ if(a[i][bwd] != '.'){ break; } } if(bwd >= 0){//come from break, not qualified continue; } while((fwd += (c + 1)) <= upper_bound){ if(a[i][fwd] != '.'){ break; } } if(fwd <= upper_bound){//come from break, not qualified ;//do nothing, loop again, find another one }else{ a[i][j] = '#'; return true; } } } } //check 2#... // for(int i = 0; i < r; i++){ for(int j = begin; j <= end; j++){ if(a[i][j] != 'o'){ continue; } for(int s = i; s < r; s++){ for(int t = (s == i ? j + 1: begin); t <= end; t++){ if(a[s][t] != 'o'){ continue; } int fwd1 = j, bwd1 = j;//1--ith row int fwd2 = t, bwd2 = t;//2--jth row while((bwd1 -= (c + 1)) >= 0){ bwd2 -= (c + 1); if(a[i][bwd1] != '.' && a[s][bwd2] != '.'){ //not qualified, because not unique.本来这里的判断写成== 'o',这个不对,因为有些已经被改成#了 break; } } if(bwd1 >= 0){//from break continue; } while((fwd1 += (c + 1)) <= upper_bound){ fwd2 += c + 1; if(a[i][fwd1] != '.'&& a[s][fwd2] != '.'){ break; } } if(fwd1 <= upper_bound){//come from break, not qualified ;//do nothing, loop again, find another one }else{ a[i][j] = '#'; a[s][t] = '#'; return true; } } } } } return false; } int main() { int i; int cnt = 1; while(scanf("%d%d%d\n", &n, &r, &c) != EOF && n != 0){ printf("Test %d\n", cnt++); upper_bound = (c + 1) * (n - 1) + c - 1;//last element for(i = 0; i < r; i++){ fgets(a[i], 81, stdin); } for(i = 0; i < n; i++){ if(!search(i)){//check whether symbol i has unique # printf("impossible\n"); break; } } if(i == n){ for(i = 0; i < r; i++){ printf("%s", a[i]); } } } return 0; }
//下面这个把search函数2个while循环变为1个while循环和一个for循环,更好理解一点,效率低了一点点,代码少了13行,main没变
View Code
1 //double vision搜索 2 #include <cstdio> 3 char a[10][81]; 4 int n, r, c;//n--num of symbols, r/c--num of rows/cols in each grid 5 int upper_bound; 6 bool search(int x){ 7 int begin = x * (c + 1); 8 int end = begin + c - 1;//inclusive 9 for(int i = 0; i < r; i++){ 10 for(int j = begin; j <= end; j++){ 11 if(a[i][j] == 'o'){ 12 int k = j;//forward,backward 13 while(k >= c + 1){ 14 k -= (c + 1); 15 } 16 for(; k <= upper_bound; k += c + 1){ 17 if(a[i][k] != '.' && k != j){ 18 break; 19 } 20 } 21 if(k <= upper_bound){//find another 22 continue; 23 }else{ 24 a[i][j] = '#'; 25 return true; 26 } 27 } 28 } 29 } 30 //check 2#... 31 // 32 for(int i = 0; i < r; i++){ 33 for(int j = begin; j <= end; j++){ 34 if(a[i][j] != 'o'){ 35 continue; 36 } 37 for(int s = i; s < r; s++){ 38 for(int t = (s == i ? j + 1: begin); 39 t <= end; t++){ 40 if(a[s][t] != 'o'){ 41 continue; 42 } 43 int k1 = j, k2 = t;//forward,backward 44 while(k1 >= c + 1){ 45 k1 -= (c + 1); 46 k2 -= (c + 1); 47 } 48 for(; k1 <= upper_bound; 49 k1 += c + 1, k2 += c + 1){ 50 if(a[i][k1] != '.' && a[s][k2] != '.' 51 && k1 != j){ 52 break; 53 } 54 } 55 if(k1 <= upper_bound){//find another 56 continue; 57 }else{ 58 a[i][j] = '#'; 59 a[s][t] = '#'; 60 return true; 61 } 62 } 63 } 64 } 65 } 66 return false; 67 } 68 int main() 69 { 70 int i; 71 int cnt = 1; 72 while(scanf("%d%d%d\n", &n, &r, &c) != EOF && n != 0){ 73 printf("Test %d\n", cnt++); 74 upper_bound = (c + 1) * (n - 1) + c - 1;//last element 75 for(i = 0; i < r; i++){ 76 fgets(a[i], 81, stdin); 77 } 78 for(i = 0; i < n; i++){ 79 if(!search(i)){//check whether symbol i has unique # 80 printf("impossible\n"); 81 break; 82 } 83 } 84 if(i == n){ 85 for(i = 0; i < r; i++){ 86 printf("%s", a[i]); 87 } 88 } 89 } 90 return 0; 91 }
1 //double vision搜索 2 #include <cstdio> 3 char a[10][81]; 4 int n, r, c;//n--num of symbols, r/c--num of rows/cols in each grid 5 int upper_bound; 6 bool search(int x){ 7 int begin = x * (c + 1); 8 int end = begin + c - 1;//inclusive 9 for(int i = 0; i < r; i++){ 10 for(int j = begin; j <= end; j++){ 11 if(a[i][j] == 'o'){ 12 int k = j;//forward,backward 13 while(k >= c + 1){ 14 k -= (c + 1); 15 } 16 for(; k <= upper_bound; k += c + 1){ 17 if(a[i][k] != '.' && k != j){ 18 break; 19 } 20 } 21 if(k <= upper_bound){//find another 22 continue; 23 }else{ 24 a[i][j] = '#'; 25 return true; 26 } 27 } 28 } 29 } 30 //check 2#... 31 // 32 for(int i = 0; i < r; i++){ 33 for(int j = begin; j <= end; j++){ 34 if(a[i][j] != 'o'){ 35 continue; 36 } 37 for(int s = i; s < r; s++){ 38 for(int t = (s == i ? j + 1: begin); 39 t <= end; t++){ 40 if(a[s][t] != 'o'){ 41 continue; 42 } 43 int k1 = j, k2 = t;//forward,backward 44 while(k1 >= c + 1){ 45 k1 -= (c + 1); 46 k2 -= (c + 1); 47 } 48 for(; k1 <= upper_bound; 49 k1 += c + 1, k2 += c + 1){ 50 if(a[i][k1] != '.' && a[s][k2] != '.' 51 && k1 != j){ 52 break; 53 } 54 } 55 if(k1 <= upper_bound){//find another 56 continue; 57 }else{ 58 a[i][j] = '#'; 59 a[s][t] = '#'; 60 return true; 61 } 62 } 63 } 64 } 65 } 66 return false; 67 } 68 int main() 69 { 70 int i; 71 int cnt = 1; 72 while(scanf("%d%d%d\n", &n, &r, &c) != EOF && n != 0){ 73 printf("Test %d\n", cnt++); 74 upper_bound = (c + 1) * (n - 1) + c - 1;//last element 75 for(i = 0; i < r; i++){ 76 fgets(a[i], 81, stdin); 77 } 78 for(i = 0; i < n; i++){ 79 if(!search(i)){//check whether symbol i has unique # 80 printf("impossible\n"); 81 break; 82 } 83 } 84 if(i == n){ 85 for(i = 0; i < r; i++){ 86 printf("%s", a[i]); 87 } 88 } 89 } 90 return 0; 91 }