杭电 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 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 move
only on black tiles.

Write a program to count the number of black tiles which he can reach by repeating the moves described above.

Input

The input consists of multiple data sets. A data set starts with a line containing two positive integers W and H; W and H are the numbers of tiles in the x- and y- directions, respectively. W and H are not more than 20.

There are H more lines in the data set, each of which includes W characters. Each character represents the color of a tile as follows.

‘.‘ - a black tile

‘#‘ - a red tile

‘@‘ - a man on a black tile(appears exactly once in a data set)

Output

For each data set, your program should output a line which contains the number of tiles he can reach from the initial tile (including itself).

Sample Input

6 9
....#.
.....#
......
......
......
......
......
#@...#
.#..#.
11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#[email protected]#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
###.###
[email protected]
###.###
..#.#..
..#.#..
0 0
此题中@也算一个可以走位置所以答案要加1,搜索时注意标记走过的点的位置以免重复计算 还有边界也要注意

#include<iostream>
#include<cstring>
using namespace std;
int ans,n,m;
int dy[4]={0,1,0,-1};
int dx[4]={1,0,-1,0};
char a[1005][1005],b[1005][1005];
void dfs(int x,int y)
{
    int xx,yy,i;
    for(i=0;i<4;i++)
    {
        xx=x+dx[i]; //此处注意不能直接用x+=d[x];否则回溯时不会回到x的初始值
        yy=y+dy[i];
        if(a[xx][yy]=='.'&&xx>=0&&xx<m&&yy>=0&&yy<n)
        {

            ans++;
            a[xx][yy]='#';
            dfs(xx,yy);
        }
    }
}
int main()
{
    int i,j;
    memset(b,0,sizeof(b));
    while(cin>>n>>m,m&&n)
    {
        ans=0;
        for(i=0;i<m;i++)
            for(j=0;j<n;j++)
            cin>>a[i][j];
        for(i=0;i<m;i++)
            for(j=0;j<n;j++)
                if(a[i][j]=='@')
                {a[i][j]='#';dfs(i,j);}

/*cout<<endl;
for(i=0;i<m;i++)
 {
      for(j=0;j<n;j++)
    cout<<a[i][j];
cout<<endl;
 }*/
            cout<<ans+1<<endl;

    }

    return 0;
}

杭电 1312 red and black

时间: 2024-07-30 08:37:07

杭电 1312 red and black的相关文章

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

这一题,简单的广搜或者深搜都可以搞定,时间复杂度都差不多. 我用的是广搜.题目的意思是:@是一个人的起始位置,#不可以走,. 可以走,求出可以走的位置的个数. 一开始没有用结构体来存储坐标,直接用的是z = x * 10 + y:将z入队,结果错了,原因是在取余整除的时候会出错.改用结构体就OK了. 下面是AC的代码: #include <iostream> #include <queue> #include <cstdio> using namespace std;

杭电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

杭电ACM题目分类

杭电ACM题目分类 基础题:1000.1001.1004.1005.1008.1012.1013.1014.1017.1019.1021.1028. 1029.1032.1037.1040.1048.1056.1058.1061.1070.1076.1089.1090.1091.1092. 1093.1094.1095.1096.1097.1098.1106.1108.1157.1163.1164.1170.1194.1196. 1197.1201.1202.1205.1219.1234.123

杭电 2060

Snooker Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 6707    Accepted Submission(s): 2839 Problem Description background: Philip likes to play the QQ game of Snooker when he wants a relax, t

杭电 HDU 1164 Eddy&#39;s research I

Eddy's research I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7117    Accepted Submission(s): 4268 Problem Description Eddy's interest is very extensive, recently  he is interested in prime

hdu 1016 Prime Ring Problem DFS解法 纪念我在杭电的第一百题

Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 29577    Accepted Submission(s): 13188 Problem Description A ring is compose of n circles as shown in diagram. Put natural num

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杭电2066【dijkstra算法】

http://acm.hdu.edu.cn/showproblem.php?pid=2066 Problem Description 虽然草儿是个路痴(就是在杭电待了一年多,居然还会在校园里迷路的人,汗~),但是草儿仍然很喜欢旅行,因为在旅途中 会遇见很多人(白马王子,^0^),很多事,还能丰富自己的阅历,还可以看美丽的风景--草儿想去很多地方,她想要去东京铁塔看夜景,去威尼斯看电影,去阳明山上看海芋,去纽约纯粹看雪景,去巴黎喝咖啡写信,去北京探望孟姜女--眼看寒假就快到了,这么一大段时间,可不