HDU 1312 Red and Black

6人阅读 评论(0) 收藏 举报
分类:

题意:根据地图,问可以到达多少地方.'@'为起点, ' . '为路,可以到达, ' # '为墙,不能通过.

思路:简单题,既可以用深搜,也可以用广搜,广搜是可以用C++里的queue,如果是C的话也可以实现其功能,其实也没有多复杂.

注意:输入的两个数,第一个是列数,第二个是行数!!!

还有一个小技巧,搜索过的地方可以用'#'标记!

#include <iostream>
#include <cstring>
#include <cmath>

using namespace std;

#define N 21

char W[N][N];//记录标点符号
int flag[N][N];//判断是否用过
int A, B;//行数和列数
int sum;//统计能走多少个
int x, y;//记录@出现的坐标x和y
int xy[4][2] = {-1,0,1,0,0,1,0,-1};//北、东、西、南 方向
void  DFS(int i, int j)
{
    flag[i][j] = 1;
    for(int k = 0; k < 4; k++){
        int a = i + xy[i][0];
        int b = j + xy[i][1];
        if( (0 <= a) && (0 <= b) && (a <= A) && (b <= B) &&(!flag[a][b]) && (W[a][b] == '.') )
        {
            ++sum;
            DFS(a,b);
        }
    }
}

int main()
{
    cin>>A>>B;
    for(int i = 1; i <= A; i++){
        for(int j = 1; j <= B; j++){
            cin>>W[i][j];
            if(W[i][j] == '@'){
                x = i;
                y = j;
            }
        }
    }
    memset(flag,0,sizeof(flag));
    sum = 1;
    DFS(x, y);

    cout<<sum<<endl;

    return 0;
}

查看评论

HDU 1312 Red and Black(经典搜索,DFS&BFS三种方式)

Red and Black Problem Description There is a rectangular room, covered with square tiles. Each...
  • hurmishine
  • hurmishine
  • 2016-03-18 23:19:49
  • 1585

杭电 HDU 1312 Red and Black(超级简单dfs)

Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...
  • lsgqjh
  • lsgqjh
  • 2015-07-31 10:22:43
  • 753

杭电(hdu)ACM 1312 Red and Black

Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...
  • IT142546355
  • IT142546355
  • 2015-08-13 08:31:17
  • 597

杭电-1312 Red and Black(DFS)

Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)...
  • lu_1110
  • lu_1110
  • 2016-07-29 11:48:06
  • 127

HDU 1312 Red and Black(水题)

A - Red and Black          There is a rectangular room, covered with square tiles. Each tile is co...
  • qq_36368339
  • qq_36368339
  • 2016-12-30 00:52:50
  • 362

杭电 1312 Red and Black 递归 附翻译

Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To...
  • qq_24678203
  • qq_24678203
  • 2015-08-03 11:19:19
  • 547

HDU 1312 Red and Black(简单BFS)

HDU1312 Red and Black(简单BFS) http://acm.hdu.edu.cn/showproblem.php?pid=1312 题意:给你一个R*C的网格,网格只有黑格和红格...
  • u013480600
  • u013480600
  • 2014-05-28 11:53:35
  • 787

Red and Black(HDU 1312)

此题链接单击这里=================#include #include using namespace std; char a[25][25]; int b[25][25]; int...
  • Folieshell
  • Folieshell
  • 2015-07-23 16:29:40
  • 168

HDU 1312 Red and Black

Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...
  • u014445884
  • u014445884
  • 2014-08-10 14:09:45
  • 220

hdu 1312 Red and Black

Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...
  • fyxz1314
  • fyxz1314
  • 2014-07-06 16:12:15
  • 447
    个人资料
    持之以恒
    等级:
    访问量: 6234
    积分: 1725
    排名: 2万+
    文章存档
    最新评论