DFS:Red and Black(POJ 1979)

               

                红与黑

   题目大意:一个人在一个矩形的房子里,可以走黑色区域,不可以走红色区域,从某一个点出发,他最多能走到多少个房间?

   不多说,DFS深搜即可,水题

   注意一下不要把行和列搞错就好了,我就是那样弄错过一次哈哈哈哈

   

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #define MAX_N 20
 4
 5 static int dp[MAX_N][MAX_N];
 6 static int startx;
 7 static int starty;
 8 static int ans;
 9
10 void DFS_Search(const int, const int, const int, const int);
11
12 int main(void)
13 {
14     int W, H, i, j;
15     while (~scanf("%d%d", &W, &H))
16     {
17         if (W == 0 && H == 0)
18             break;
19         ans = 0;//先算起点的
20         for (i = 0; i < H; i++)//Read Map
21         {
22             getchar();
23             for (j = 0; j < W; j++)
24             {
25                 scanf("%c", &dp[i][j]);
26                 if (dp[i][j] == ‘@‘){
27                     startx = i; starty = j;
28                 }
29             }
30         }
31         DFS_Search(startx, starty, W, H);
32         printf("%d\n", ans);
33     }
34
35     return 0;
36 }
37
38 void DFS_Search(const int x, const int y, const int W, const int H)
39 {
40     dp[x][y] = ‘#‘;
41     ans++;
42     if (x - 1 >= 0 && dp[x - 1][y] != ‘#‘)
43         DFS_Search(x - 1, y, W, H);
44     if (x + 1 < H && dp[x + 1][y] != ‘#‘)
45         DFS_Search(x + 1, y, W, H);
46     if (y - 1 >= 0 && dp[x][y - 1] != ‘#‘)
47         DFS_Search(x, y - 1, W, H);
48     if (y + 1 < W && dp[x][y + 1] != ‘#‘)
49         DFS_Search(x, y + 1, W, H);
50 }
时间: 2024-12-26 02:03:41

DFS:Red and Black(POJ 1979)的相关文章

Red and Black POJ - 1979(题解)

原题 http://poj.org/problem?id=1979 题目大意 题目是讲给一张图,然后这张图里一个'@'的字符表示起点,'.'是黑瓦(可移动到该位置),'#'是红瓦片(不可移到该位置),然后问可以到达的位置有多少个,包括起点. 题目分析 dfs模板题,可以当作练习基本的dfs,读入地图的时候顺便记录起点的位置,然后从起点开始dfs,走过的位置顺便标记为'#',防止重复走,记录走过的步数即可.具体dfs可看代码. 代码 1 #include <cstdio> 2 #include

《挑战》2.1 POJ POJ 1979 Red and Black (简单的DFS)

B - Red and Black Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 1979 Description There is a rectangular room, covered with square tiles. Each tile is col

POJ 1979 Red and Black (红与黑)

POJ 1979 Red and Black (红与黑) Time Limit: 1000MS    Memory Limit: 30000K 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

POJ 1979 Red and Black 深度优先搜索上手题

Red and Black Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 21738   Accepted: 11656 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

poj 1979 &amp;&amp; zoj 2165 Red and Black

Red and Black Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 22409   Accepted: 12100 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

POJ 1979 Red and Black【深度优先搜索】

题目链接:http://poj.org/problem?id=1979 题目大意:一个矩形的房间地板被分为w*h个小块,每一个小块不是红的就是黑的,你首先站在一个黑色小块上,你只能朝你的四个方向(上下左右)移动,且不能到达红色的小块上,问你最多能到达多少个小块. 很简单的dfs深度优先搜索 没搜索过一个格子,将该格子设置为红色,之后的搜索就不会再搜索到该格子,就不会造成重复,因为该题有很多数据,记得每次处理数据是初始化各数组及其他数据. 代码如下: #include <iostream> #i

POJ 1979 POJ 3009 AOJ 0033 AOJ 0118 [搜索类题目][0033贪心模拟]

/** POJ 1979 BFS */ #include <stdio.h> #include <string.h> #include <iostream> #include <queue> using namespace std; const int N = 20 + 5; int mp[N][N]; int sx,sy; int n, m; int vis[3000]; int dirx[] = {0, 1, 0, -1}; int diry[] = {

POJ 1979 Red and Black dfs 难度:0

http://poj.org/problem?id=1979 #include <cstdio> #include <cstring> using namespace std; const int maxn = 21; bool vis[maxn][maxn]; char maz[maxn][maxn]; int n,m; const int dx[4] = {1,-1,0,0}; const int dy[4] = {0,0,1,-1}; int ans; bool in(int

POJ 1979 Red and Black(简单DFS)

Red and Black 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