记得很久以前做过这个题目的,那时候记得应该实在福州oj上做过的。
Time Limit: 2 second(s) | Memory Limit: 32 MB |
Once there was a king named Akbar. He had a son named Shahjahan. For an unforgivable reason the king wanted him to leave the kingdom. Since he loved his son he decided his son would be banished in a new place. The prince became sad, but he followed his father's will. In the way he found that the place was a combination of land and water. Since he didn't know how to swim, he was only able to move on the land. He didn't know how many places might be his destination. So, he asked your help.
For simplicity, you can consider the place as a rectangular grid consisting of some cells. A cell can be a land or can contain water. Each time the prince can move to a new cell from his current position if they share a side.
Now write a program to find the number of cells (unit land) he could reach including the cell he was living.
Input
Input starts with an integer T (≤ 500), denoting the number of test cases.
Each case starts with a line containing two positive integers W and H; W and H are the numbers of cells in the x and y directions, respectively. W and H are not more than 20.
There will be H more lines in the data set, each of which includes W characters. Each character represents the status of a cell as follows.
1) '.' - land
2) '#' - water
3) '@' - initial position of prince (appears exactly once in a dataset)
Output
For each case, print the case number and the number of cells he can reach from the initial position (including it).
Sample Input |
Output for Sample Input |
4 6 9 ....#. .....# ...... ...... ...... ...... ...... #@...# .#..#. 11 9 .#......... .#.#######. .#.#.....#. .#.#.###.#. .#.#..@#.#. .#.#####.#. .#.......#. .#########. ........... 11 6 ..#..#..#.. ..#..#..#.. ..#..#..### ..#..#..#@. ..#..#..#.. ..#..#..#.. 7 7 ..#.#.. ..#.#.. ###.### ...@... ###.### ..#.#.. ..#.#.. |
Case 1: 45 Case 2: 59 Case 3: 6 Case 4: 13 |
1 // Project name : 1012 ( 1012 Guilty Prince ) 2 // File name : main.cpp 3 // Author : iCoding 4 // E-mail : honi.linux@gmail.com 5 // Date & Time : Wed Aug 8 19:11:24 2012 6 7 8 #include <iostream> 9 #include <stdio.h> 10 #include <string> 11 #include <cmath> 12 #include <algorithm> 13 using namespace std; 14 15 /*************************************************************************************/ 16 /* data */ 17 #ifndef MAXN 18 #define MAXN 30 19 #endif 20 21 const char CH_EMPTY = '.'; 22 const char CH_START = '@'; 23 const char CH_BLOCK = '#'; 24 25 int n, m; 26 27 char iMap[MAXN][MAXN]; 28 29 int iAnswer; 30 31 int dir_x[4] = {0 , 0, -1, 1}; 32 int dir_y[4] = {-1, 1, 0, 0}; 33 34 int start_x; 35 int start_y; 36 /*************************************************************************************/ 37 /* procedure */ 38 void debug() 39 { 40 cout << "--debug msg--" << endl; 41 } 42 void iInit() 43 { 44 /*set all memory of iMap to CH_BLOCK*/ 45 for (int i = 0; i < MAXN; i++) 46 { 47 for (int j = 0; j < MAXN; j++) 48 { 49 iMap[i][j] = CH_BLOCK; 50 } 51 } 52 53 /*init for iAnswer*/ 54 iAnswer = 0; 55 56 /*init for iMap*/ 57 for (int i = 1; i <= n; i++) 58 { 59 string line; 60 cin >> line; 61 //cout << line << endl; 62 for (int j = 1; j <= m; j++) 63 { 64 if (line[j-1] == CH_EMPTY) 65 { 66 iMap[i][j] = CH_EMPTY; 67 } 68 else if (line[j-1] == CH_BLOCK) 69 { 70 iMap[i][j] = CH_BLOCK; 71 } 72 else if (line[j-1] == CH_START) 73 { 74 iMap[i][j] = CH_EMPTY; 75 start_x = i; 76 start_y = j; 77 } 78 } 79 } 80 } 81 82 bool iCanGo(int x, int y) 83 { 84 if (iMap[x][y] == CH_BLOCK) 85 { 86 return false; 87 } 88 else 89 { 90 return true; 91 } 92 } 93 94 void iDFS(int x, int y) 95 { 96 //debug(); 97 cout << "--" << x << " " << y << endl; 98 /*iMap[x][y] = CH_BLOCK; 99 iAnswer++; 100 for (int iDirCase = 0; iDirCase < 4; iDirCase++) 101 { 102 if (iCanGo(x + dir_x[iDirCase] , y + dir_y[iDirCase])) 103 { 104 iDFS(x + dir_x[iDirCase] , y + dir_y[iDirCase]); 105 } 106 }*/ 107 108 bool iFlagGo[4] = {false, false, false, false}; 109 for (int iDirCase = 0; iDirCase < 4; iDirCase++) 110 { 111 if (iCanGo(x + dir_x[iDirCase] , y + dir_y[iDirCase])) 112 { 113 iFlagGo[iDirCase] = true; 114 iMap[x + dir_x[iDirCase]][y + dir_y[iDirCase]] = CH_BLOCK; 115 iAnswer++; 116 } 117 } 118 119 for (int iDirCase = 0; iDirCase , 4; iDirCase++) 120 { 121 if (iFlagGo[iDirCase]) 122 { 123 iDFS(x + dir_x[iDirCase] , y + dir_y[iDirCase]); 124 } 125 } 126 } 127 128 129 /*************************************************************************************/ 130 /* main */ 131 int main() 132 { 133 int iT; 134 cin >> iT; 135 for (int iCaseCount = 1; iCaseCount <= iT; iCaseCount++) 136 { 137 cout << "Case " << iCaseCount << ": "; 138 cin >> m >> n; 139 iInit(); 140 iMap[start_x][start_y] = CH_BLOCK; 141 iAnswer = 1; 142 iDFS(start_x, start_y); 143 cout << iAnswer << endl; 144 } 145 return 0; 146 } 147 148 // end 149 // Code by Sublime text 2 150 // iCoding@CodeLab
本来今晚是
2012阿里云“凌云杯”高校编程大赛 - 热身赛(时间待定)
可是进去没题目的。。搞毛啊。。。所以还是自己找题做咯。。
先上自己代码。等下AC继续。。。
终于AC了。。原来在119杭的iDirCase < 4写成了iDirCase , 4。。。罪过罪过。。害我调试那么久。。
1 // Project name : 1012 ( 1012 Guilty Prince ) 2 // File name : main.cpp 3 // Author : iCoding 4 // E-mail : honi.linux@gmail.com 5 // Date & Time : Wed Aug 8 19:11:24 2012 6 7 8 #include <iostream> 9 #include <stdio.h> 10 #include <string> 11 #include <cmath> 12 #include <algorithm> 13 using namespace std; 14 15 /*************************************************************************************/ 16 /* data */ 17 #ifndef MAXN 18 #define MAXN 30 19 #endif 20 21 const char CH_EMPTY = '.'; 22 const char CH_START = '@'; 23 const char CH_BLOCK = '#'; 24 25 int n, m; 26 27 char iMap[MAXN][MAXN]; 28 29 int iAnswer; 30 31 int dir_x[4] = {0 , 0, -1, 1}; 32 int dir_y[4] = {-1, 1, 0, 0}; 33 34 int start_x; 35 int start_y; 36 /*************************************************************************************/ 37 /* procedure */ 38 void debug() 39 { 40 cout << "--debug msg--" << endl; 41 } 42 void iInit() 43 { 44 /*set all memory of iMap to CH_BLOCK*/ 45 for (int i = 0; i < MAXN; i++) 46 { 47 for (int j = 0; j < MAXN; j++) 48 { 49 iMap[i][j] = CH_BLOCK; 50 } 51 } 52 53 /*init for iAnswer*/ 54 iAnswer = 0; 55 56 /*init for iMap*/ 57 for (int i = 1; i <= n; i++) 58 { 59 string line; 60 cin >> line; 61 //cout << line << endl; 62 for (int j = 1; j <= m; j++) 63 { 64 if (line[j-1] == CH_EMPTY) 65 { 66 iMap[i][j] = CH_EMPTY; 67 } 68 else if (line[j-1] == CH_BLOCK) 69 { 70 iMap[i][j] = CH_BLOCK; 71 } 72 else if (line[j-1] == CH_START) 73 { 74 iMap[i][j] = CH_EMPTY; 75 start_x = i; 76 start_y = j; 77 } 78 } 79 } 80 } 81 82 bool iCanGo(int x, int y) 83 { 84 if (iMap[x][y] == CH_BLOCK) 85 { 86 return false; 87 } 88 else 89 { 90 return true; 91 } 92 } 93 94 void iDFS(int x, int y) 95 { 96 //debug(); 97 //cout << "--" << x << " " << y << endl; 98 /*iMap[x][y] = CH_BLOCK; 99 iAnswer++; 100 for (int iDirCase = 0; iDirCase < 4; iDirCase++) 101 { 102 if (iCanGo(x + dir_x[iDirCase] , y + dir_y[iDirCase])) 103 { 104 iDFS(x + dir_x[iDirCase] , y + dir_y[iDirCase]); 105 } 106 }*/ 107 108 bool iFlagGo[4] = {false, false, false, false}; 109 for (int iDirCase = 0; iDirCase < 4; iDirCase++) 110 { 111 if (iCanGo(x + dir_x[iDirCase] , y + dir_y[iDirCase])) 112 { 113 iFlagGo[iDirCase] = true; 114 iMap[x + dir_x[iDirCase]][y + dir_y[iDirCase]] = CH_BLOCK; 115 iAnswer++; 116 } 117 } 118 119 for (int iDirCase = 0; iDirCase < 4; iDirCase++) 120 { 121 if (iFlagGo[iDirCase]) 122 { 123 iDFS(x + dir_x[iDirCase] , y + dir_y[iDirCase]); 124 } 125 } 126 } 127 128 129 /*************************************************************************************/ 130 /* main */ 131 int main() 132 { 133 int iT; 134 cin >> iT; 135 for (int iCaseCount = 1; iCaseCount <= iT; iCaseCount++) 136 { 137 cout << "Case " << iCaseCount << ": "; 138 cin >> m >> n; 139 iInit(); 140 iMap[start_x][start_y] = CH_BLOCK; 141 iAnswer = 1; 142 iDFS(start_x, start_y); 143 cout << iAnswer << endl; 144 } 145 return 0; 146 } 147 148 // end 149 // Code by Sublime text 2 150 // iCoding@CodeLab