HDU-1312-Red and Black

题目链接

http://acm.hdu.edu.cn/showproblem.php?pid=1312

此题注意n,m 表示到底是行还是列

代码

#include<stdio.h>

#include<string.h>

int n,m,cnt;

char s[30][30];

int to[4][2]={{1,0},{0,1},{-1,0},{0,-1}};

void dfs(int i,int j)

{

cnt++;

s[i][j]=‘#‘;

int k;

for(k=0;k<4;k++)

{

int x=i+to[k][0];

int y=j+to[k][1];

if(x>=0&&x<n&&y>=0&&y<m&&s[x][y]==‘.‘)

dfs(x,y);

}

}

int main(void)

{

int i,j;

int fi,fj;

while(scanf("%d%d",&m,&n)==2&&(n+m))

{

getchar();

for(i=0;i<n;i++)

{

for(j=0;j<m;j++)

{

scanf("%c",&s[i][j]);

if(s[i][j]==‘@‘)

{

fi=i;

fj=j;

}

}

getchar();

}

cnt=0;

s[fi][fj]=‘#‘;

dfs(fi,fj);

printf("%d\n",cnt);

}

return 0;

}

时间: 2024-10-17 22:17:14

HDU-1312-Red and Black的相关文章

HDU 1312 ----- Red and Black 入门搜索 DFS解法

HDU 1312 ----- Red and Black  入门搜索  http://acm.hdu.edu.cn/showproblem.php?pid=1312 /*HDU 1312 ----- Red and Black 入门搜索 */ #include <cstdio> int n, m; //n行m列 int cnt, startx, starty; char mapp[25][25]; /*找一个连通的区域里可以走的块*/ void dfs(int x, int y){ if (x

HDU 1312 Red and Black (搜索)

Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 9795    Accepted Submission(s): 6103 Problem Description There is a rectangular room, covered with square tiles. Each tile is colore

hdu 1312 Red and Black(BFS水题)

Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 9684    Accepted Submission(s): 6021 Problem Description There is a rectangular room, covered with square tiles. Each tile is colore

HDU 1312:Red and Black(DFS搜索)

HDU 1312:Red and Black Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u Description There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is standing on a black tile. From

HDU 1312 Red and Black (dfs)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312 Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 17773    Accepted Submission(s): 10826 Problem Description There is a rectangula

HDU 1312 Red and Black(bfs,dfs均可,个人倾向bfs)

题目代号:HDU 1312 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312 Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 20820    Accepted Submission(s): 12673 Problem Description There i

hdu 1312 Red and Black (bfs)

Problem Description There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is standing on a black tile. From a tile, he can move to one of four adjacent tiles. But he can't move on red tiles, he can mo

HDU 1312 Red and Black(基础bfs或者dfs)

Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 11843    Accepted Submission(s): 7380 Problem Description There is a rectangular room, covered with square tiles. Each tile is color

HDU 1312 Red and Black【深搜】

Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 11531    Accepted Submission(s): 7177 Problem Description There is a rectangular room, covered with square tiles. Each tile is colo

hdu 1312 Red and Black(深搜)

Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 10286    Accepted Submission(s): 6426 Problem Description There is a rectangular room, covered with square tiles. Each tile is colo