UVA10010 - Where's Waldorf?
题目http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=951
就是从每个字符的8个方向循环找是否有跟给出的字符串相等的 找的时候稍微限制一些条件即可 代码有点长
View Code
1 #include<stdio.h> 2 #include<string.h> 3 int main() 4 { 5 int t, n, m, k, y, i, j,f,x,w,v,flag,g,o; 6 char c[55][55],str[55]; 7 scanf("%d", &t); 8 while(t--) 9 { 10 scanf("%d%d%*c", &n, &m); 11 for(i = 1 ; i <= n ; i++) 12 { 13 for(j = 1 ; j <= m ; j++) 14 scanf("%c", &c[i][j]); 15 getchar(); 16 } 17 scanf("%d", &o); 18 getchar(); 19 while(o--) 20 { 21 gets(str); 22 k = strlen(str); 23 for(i = 1 ; i <= n ; i++) 24 { 25 for(j = 1 ; j <= m ; j++) 26 { 27 w = i; 28 v = j; 29 flag = 0; 30 if(j>=k) 31 { 32 flag = 1; 33 y = j; 34 for(g = 0; g < k ; g++) 35 { 36 if(str[g] != c[i][y]&&(str[g]-c[i][y]!=32&&str[g]-c[i][y]!=-32)) 37 { 38 flag = 0; 39 break; 40 } 41 y--; 42 } 43 } 44 45 if(flag == 1) 46 break; 47 if((j+k-1)<=m) 48 { 49 flag = 1; 50 y = j; 51 for(g = 0; g < k ; g++) 52 { 53 if(str[g] != c[i][y]&&(str[g]-c[i][y]!=32&&str[g]-c[i][y]!=-32)) 54 { 55 flag = 0; 56 break; 57 } 58 y++; 59 } 60 } 61 if(flag == 1) 62 break; 63 64 if(i>=k) 65 { 66 flag = 1; 67 y = i; 68 for(g = 0; g < k ; g++) 69 { 70 if(str[g] != c[y][j]&&(str[g]-c[y][j]!=32&&str[g]-c[y][j]!=-32)) 71 { 72 flag = 0; 73 break; 74 } 75 y--; 76 } 77 } 78 if(flag == 1) 79 break; 80 81 if(i+k-1<=n) 82 { 83 84 flag = 1; 85 y = i; 86 for(g = 0; g < k ; g++) 87 { 88 if(str[g] != c[y][j]&&(str[g]-c[y][j]!=32&&str[g]-c[y][j]!=-32)) 89 { 90 flag = 0; 91 break; 92 } 93 y++; 94 } 95 } 96 if(flag == 1) 97 break; 98 99 if(j>=k&&i>=k) 100 { 101 flag = 1; 102 f = i; 103 x = j; 104 for(g = 0 ; g < k ; g++) 105 { 106 if(str[g] != c[f][x]&&(str[g]-c[f][x]!=32&&str[g]-c[f][x]!=-32)) 107 { 108 flag = 0; 109 break; 110 } 111 f--; 112 x--; 113 } 114 } 115 if(flag == 1) 116 break; 117 118 if(j+k-1<=m&&i>=k) 119 {flag = 1; 120 f = i; 121 x = j; 122 for(g = 0 ; g < k ; g++) 123 { 124 if(str[g] != c[f][x]&&(str[g]-c[f][x]!=32&&str[g]-c[f][x]!=-32)) 125 { 126 flag = 0; 127 break; 128 } 129 f--; 130 x++; 131 } 132 } 133 if(flag == 1) 134 break; 135 136 if(j<=k&&i+k-1<=n) 137 {flag = 1; 138 f = i; 139 x = j; 140 for(g = 0 ; g < k ; g++) 141 { 142 if(str[g] != c[f][x]&&(str[g]-c[f][x]!=32&&str[g]-c[f][x]!=-32)) 143 { 144 flag = 0; 145 break; 146 } 147 f++; 148 x--; 149 } 150 } 151 if(flag == 1) 152 break; 153 154 if(j+k-1<=m&&i+k-1<=n) 155 {flag = 1; 156 f = i; 157 x = j; 158 for(g = 0 ; g < k ; g++) 159 { 160 161 if(str[g] != c[f][x]&&(str[g]-c[f][x]!=32&&str[g]-c[f][x]!=-32)) 162 { 163 flag = 0; 164 break; 165 } 166 f++; 167 x++; 168 } 169 } 170 if(flag == 1) 171 break; 172 } 173 if(flag == 1) 174 break; 175 } 176 177 printf("%d %d\n",w,v); 178 } 179 if(t!=0) 180 puts(""); 181 } 182 return 0; 183 }