杭电ACM1312——Red and Black~~广搜

这一题,简单的广搜或者深搜都可以搞定,时间复杂度都差不多。

我用的是广搜。题目的意思是:@是一个人的起始位置,#不可以走,. 可以走,求出可以走的位置的个数。

一开始没有用结构体来存储坐标,直接用的是z = x * 10 + y;将z入队,结果错了,原因是在取余整除的时候会出错。改用结构体就OK了。

下面是AC的代码:

#include <iostream>
#include <queue>
#include <cstdio>
using namespace std;

class data
{
public:
	int x, y;
};

char map[25][25];
int W, H;
int start_x, start_y;
int xy[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};

int bfs()
{
    int ans = 0;
    queue <data> Que;
    data tem;
	tem.x = start_x; tem.y = start_y;
    map[start_x][start_y] = '#';
    Que.push(tem);
    while(!Que.empty())
    {
        tem = Que.front();
        Que.pop();
        ans++;

        data temp;
        for(int i = 0; i < 4; i++)
        {
            temp.x = tem.x + xy[i][0];
			temp.y = tem.y + xy[i][1];
            if(temp.x >= 0 && temp.x < H && temp.y >= 0 && temp.y < W && map[temp.x][temp.y] == '.')
            {
                map[temp.x][temp.y] = '#';
                Que.push(temp);
            }
        }
    }
    return ans;
}

int main()
{
//	freopen("data.txt", "r", stdin);
    while(scanf("%d%d", &W, &H) != EOF)
    {
        getchar();
        if(W == 0 && H == 0)
            break;
        for(int i = 0; i < H; i++)
        {
            for(int j = 0; j < W; j++)
            {
                scanf("%c", &map[i][j]);
                if(map[i][j] == '@')
                {
                    start_x = i;
                    start_y = j;
                }
            }
            getchar();
        }
        int ans = bfs();
        printf("%d\n", ans);
    }
    return 0;
}
时间: 2024-08-23 21:33:22

杭电ACM1312——Red and Black~~广搜的相关文章

杭电 1372 Knight Moves(广搜模板题)

http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6439    Accepted Submission(s): 3886 Problem Description A friend of you is doing res

杭电1175——连连看~简单的广搜

这一题,做了好久,终于AC了,感觉题目有点坑,唉,题目不是很难,就是坑!!-- 找一个错误,找了半天,后来才看到是变量用错了!!~ 题目中的b数组是标记数组. #include <iostream> #include <cstdio> #include <queue> using namespace std; #define INF 1000000 int xy[4][2] = { { -1 , 0 } , { 1 , 0 } , { 0 , -1 } , { 0 ,

杭电 1312 red and black

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

杭电 1242 Rescue(广搜)

http://acm.hdu.edu.cn/showproblem.php?pid=1242 Rescue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 15597    Accepted Submission(s): 5663 Problem Description Angel was caught by the MOLIGPY!

杭电 1518 Square (深搜)(回溯)

http://acm.hdu.edu.cn/showproblem.php?pid=1518 Square Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 8343    Accepted Submission(s): 2706 Problem Description Given a set of sticks of various

杭电ACM分类

杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze 广度搜索1006 Redraiment猜想 数论:容斥定理1007 童年生活二三事 递推题1008 University 简单hash1009 目标柏林 简单模拟题1010 Rails 模拟题(堆栈)1011 Box of Bricks 简单题1012 IMMEDIATE DECODABILITY

【转】对于杭电OJ题目的分类

[好像博客园不能直接转载,所以我复制过来了..] 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze 广度搜索1006 Redraiment猜想 数论:容斥定理1007 童年生活二三事 递推题1008 University 简单hash1009 目标柏林 简单模拟题1010 Rails 模拟题(堆栈)1011 Box of Bricks 简单题1012 IMMEDI

杭电1253 胜利大逃亡

胜利大逃亡 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 24756    Accepted Submission(s): 9478 Problem Description Ignatius被魔王抓走了,有一天魔王出差去了,这可是Ignatius逃亡的好机会. 魔王住在一个城堡里,城堡是一个A*B*C的立方体,可以被表示成A个B*C的

杭电 2553 N皇后问题

http://acm.hdu.edu.cn/showproblem.php?pid=2553 N皇后问题 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 8077    Accepted Submission(s): 3603 Problem Description 在N*N的方格棋盘放置了N个皇后,使得它们不相互攻击(即任意2个皇后不