LightOJ 1012 Guilty Prince(dfs水题)

1012 – Guilty Prince

Time Limit:2 second(s)Memory Limit:32 MB

Once there was a king named Akbar. He had a son namedShahjahan. 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. Theprince became sad, but he followed his father’s will. In the way he found thatthe place was a combination of land and water. Since he didn’t know how toswim, he was only able to move on the land. He didn’t know how many placesmight be his destination. So, he asked your help.

For simplicity, you can consider the place as a rectangulargrid consisting of some cells. A cell can be a land or can contain water. Eachtime the prince can move to a new cell from his current position if they sharea 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 integerT (≤ 500),denoting the number of test cases.

Each case starts with a line containing two positiveintegersWandH;WandHare the numbers of cellsin thexandydirections, respectively.WandHare not more than 20.

There will beHmore lines in the data set, each ofwhich includesWcharacters. Each character represents the status of acell as follows.

1)’.’- land

2)’#’- water

3)’@’- initial position of prince (appears exactlyonce in a dataset)

Output

For each case, print the case number and the number of cellshe can reach from the initial position (including it).

Sample InputOutput for Sample Input

4

6 9

….#.

…..#

……

……

……

……

……

#@…#

.#..#.

11 9

.#………

.#.#######.

.#.#…..#.

.#.#.###.#.

.#.#..@#.#.

.#.#####.#.

.#…….#.

.#########.

………..

11 6

..#..#..#..

..#..#..#..

..#..#..###

..#..#..#@.

..#..#..#..

..#..#..#..

7 7

..#.#..

..#.#..

###.###

…@…

###.###

..#.#..

..#.#..

Case 1: 45

Case 2: 59

Case 3: 6

Case 4: 13

题目链接 :

题目大意 : 给个起点,求能够到达的点的数目

#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<string>#include<algorithm>#include<cstdlib>#include<set>#include<queue>#include<stack>#include<vector>#include<map>#define N 100010#define Mod 10000007#define lson l,mid,idx<<1#define rson mid+1,r,idx<<1|1#define lc idx<<1#define rc idx<<1|1const double EPS = 1e-11;const double PI = acos ( -1.0 );const double E = 2.718281828;typedef long long ll;const int INF = 1000010;using namespace std;char mp[100][100];int n,m,sx,sy,ans;int x[4]= {-1,0,1,0};int y[4]= {0,1,0,-1};void dfs(int i,int j) {if(mp[i][j]=='.') {ans++,mp[i][j]='#';}for(int ii=0;ii<4;ii++){int xx=i+x[ii];int yy=j+y[ii];if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&mp[xx][yy]=='.')dfs(xx,yy);}}int main() {int t;int ca=1;scanf("%d",&t);while(t–) {scanf("%d%d",&m,&n);getchar();for(int i=1; i<=n; i++) {for(int j=1; j<=m; j++) {scanf("%c",&mp[i][j]);if(mp[i][j]=='@') {sx=i,sy=j,mp[i][j]='.';}}getchar();}ans=0;dfs(sx,sy);printf("Case %d: %d\n",ca++,ans);}return 0;}

,你可以用爱得到全世界,你也可以用恨失去全世界

LightOJ 1012 Guilty Prince(dfs水题)

相关文章:

你感兴趣的文章:

标签云: