poj 1979 Red and Black
来源:互联网 发布:电脑锣编程代码 编辑:程序博客网 时间:2024/06/06 09:47
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.
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)
The end of the input is indicated by a line consisting of two zeros.
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)
The end of the input is indicated by a line consisting of two zeros.
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.#..........#.#######..#.#.....#..#.#.###.#..#.#..@#.#..#.#####.#..#.......#..#########............11 6..#..#..#....#..#..#....#..#..###..#..#..#@...#..#..#....#..#..#..7 7..#.#....#.#..###.###...@...###.###..#.#....#.#..0 0
Sample Output
4559613简单搜索 直接dfs即可。#include<stdio.h>#include <iostream>#include<string.h>#include <algorithm>using namespace std;int vis[25][25];char map[25][25];int w,h,sum;int xx,yy;int dx[]={1,0,-1,0};int dy[]={0,1,0,-1};void dfs(int x,int y){ for(int i=0;i<4;i++) { int x1=x+dx[i]; int y1=y+dy[i]; if(map[x1][y1]=='#')continue;//红色 if((x1<0||x1>=h)||(y1<0||y1>=w))continue;//出界 if(!vis[x1][y1])//标记过 { vis[x1][y1]=1; sum++; dfs(x1,y1); } }}int main(){ while(scanf("%d%d",&w,&h)) { if(w==0&&h==0) break; memset(vis,0,sizeof(vis)); sum=0; for(int i=0;i<h;i++) { scanf("%s",&map[i]); for(int j=0;j<w;j++) if(map[i][j]=='@') { xx=i;yy=j; } } dfs(xx,yy); if(sum) cout<<sum<<endl; else cout<<1<<endl; }}
0 0
- POJ 1979 Red and Black
- poj 1979 Red and Black
- POJ 1979 - Red and Black
- POJ-1979-Red and Black
- poj 1979 red and black
- poj 1979 Red and Black
- poj 1979 Red and Black
- POJ:1979 Red and Black
- POJ 1979 - Red and Black
- POJ 1979 Red and Black
- POJ 1979 Red and Black
- POJ 1979 Red and Black
- poj 1979 Red and Black
- POJ 1979 Red and Black
- poj 1979Red and Black
- POJ 1979 Red and Black
- POJ 1979 Red and Black
- POJ 1979-Red and Black
- AJAX
- NSURLConnection 文件下载的BUG及解决思路、方案
- PorterDuffXferMode不正确的真正原因PorterDuffXferMode深入试验)
- oracle----------数据类型,增删改
- 1.XML 简介
- poj 1979 Red and Black
- Quartz 2D
- Sudoku Solver
- 数字字符串四则运算
- Android 动画学习
- sdl显示bmp
- gdb调试正在运行进程
- Android进阶——git上传代码到coding.net
- 拓胜培训Java,每天的点点滴滴