Red and Black(搜尋題目)
Time Limit: 1000MSMemory Limit: 32768KB64bit IO Format: %I64d & %I64u
Submit
Status
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
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
###.###
[email protected]
###.###
..#.#..
..#.#..
0 0
Sample Output
45
59
6
13
Source
Asia 2004, Ehime (Japan), Japan Domestic
簡單的搜尋問題,根據題意,可以重複行走,求能到達的最多的...的數量,剛開始拿過題來以為是寬搜,因為做了幾個寬搜的題目,形成思維定視了,後來一看到重複行走就想到了深搜。
#include <cstdio> #include <iostream> #include <cmath> #include <queue> #include <algorithm> using namespace std; int dx[4]={-1,0,1,0}; int dy[4]={0,-1,0,1}; int map[40][40]; int m,n; struct node { int x; int y; int sept; }; int sum; int bfs(int a,int b) { if (map[a][b]=='.') { map[a][b]='&'; sum++; } for (int number1=0;number1<4;number1++) { int nx=a+dx[number1],ny=b+dy[number1]; if (map[nx][ny]=='.'&&nx>=0&&nx<n&&ny>=0&&ny<m) { bfs(nx,ny); } } } int main() { while(scanf("%d %d",&m,&n)!=EOF) { if (m==0&&n==0)break; int i,j; getchar(); for (int number1=0;number1<n;number1++) { for (int number2=0;number2<m;number2++) { scanf("%c",&map[number1][number2]); if (map[number1][number2]=='@') { i=number1; j=number2; } } getchar(); } sum=1; bfs(i,j); printf("%d\n",sum); } return 0; }
相關推薦
Red and Black(搜尋題目)
Red and Black Time Limit: 1000MSMemory Limit: 32768KB64bit IO Format: %I64d & %I64u Submit Status Description There is a rectangula
ZOJ—— 2165 Red and Black(搜尋)
題目: Problem Description There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is
POJ 1979 Red and Black(簡單DFS)
either www enter ont false num present direction roo Red and Black Description There is a rectangular room, covered with square tile
HDU 1312 Red and Black(bfs,dfs均可,個人傾向bfs)
spec int ger time scrip follow stdio.h stack line 題目代號:HDU 1312 題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=1312 Red and Black Time Li
【POJ】1979 Red and Black(BFS)
Red and Black Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 44023 Accepted: 23850 Description There is a
Red and Black(板子)
題目描述 題面 There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is standing on a bl
ACM:POJ-1979 Red And Black(JAVA的字元陣列輸入以及標記方法)
對於該題目直接用DFS或者BFS都可以直接暴力出來,實際上題目也不難,只需要遞迴一個搜尋方法即可。那麼用C或者C++將很容易的解決這個題目。 但是如果用JAVA寫的話將存在一個昨晚讓我糾結了很久的問題:輸入資訊需要以字元陣列的形式儲存,那麼如何解決標記以及儲存的問題? 剛開
搜尋1(廣度優先)Red and Black
Red and Black Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 14 Accepted Su
N - Red and Black ~~ [kuangbin帶你飛]專題一 簡單搜尋
有一個長方形的房間,覆蓋了正方形的磁磚。每塊磁磚的顏色,要麼是紅色,要麼是黑色。一名男子站在一塊黑色的磁磚上。他可以從一塊磁磚移至相鄰四塊磁磚中的某一塊。但是,他不允許在紅色磁磚上移動,他只允許在黑色磁磚上移動。 編寫一個程式,使得他允許重複上述的移動,判斷他所能到達的黑色磁磚的數量。 輸入
HDU 1312Red and Black(dfs)
Red and Black Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 1 Accepted Subm
hdu 3911-區間合併、更新和查詢Black And White (&& hdu 1199)
一個序列要麼是1,要麼是0. 操作: 修改:修改區間[a,b],使0變成1,1變成0; 詢問:區間[a,b]最長的連續1有多長? 解: 線段樹。 節點資訊: preb,sufb,prew,sufw,mxb,mxw;分別表示最
1.數獨題目生成程式(搜尋演算法)
前幾天在玩數獨遊戲的時候,產生了一個大膽的想法: 數獨app上的題目都是現成的,乾脆自己寫一個可以隨機生成數獨的程式算了 一、需求提出: 1.隨機生成數獨題目,要求該題目有解; 2.當填數違反數獨操作(填到題目原本的數字去了,填數違規等)時,禁止操作,彈出提示
CodeForces 321A Ciel and Robot(數學模擬)
ont amp force 等於 return printf print -- 題意 題目鏈接:http://codeforces.com/problemset/problem/321/A 題意:在一個二維平面中,開始時在(0,0)點,目標點是(a。b),問能不能通過反
HDU1312 Red and Black
fine mil scanf inpu red other mit ets represent Red and Black Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (
hdu(1069)——Monkey and Banana(LIS變形)
res 排序 inf popu 坐標 trac 保存 i++ urn 題意: 如今給你n個石塊,然後它由坐標來表示(x,y,z)。可是它能夠有不同的方法,也就是說它的三個坐標能夠輪換著來的。 石塊的數量不限,可是每次都必須保持上底面的長和寬嚴格遞減,然後問你用這些石塊所
hdoj-1312-Red and Black
man arc others sim track pid for each only red Red and Black Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav
HDU - 1312 : Red and Black
getchar() nbsp and stand oid std void tile follow There is a rectangular room, covered with square tiles. Each tile is colored either red
HDU 1979 Red and Black
pro lin itself tip 設置問題 dir repeat ons elf 題目: There is a rectangular room, covered with square tiles. Each tile is colored either red or
[BZOJ1419] Red is good(期望DP)
-1 cst com log ret brush 期望 class www. 傳送門 逆推 只不過順序還是順著的,思想是逆著的 f[i][j]表示還剩下i張紅牌,j張黑牌的期望值 那麽邊界是 f[i][0]=i,因為只剩i張紅牌 f[0][j]=0,只剩黑
【POJ】1704 Georgia and Bob(Staircase Nim)
row over diff 題目 player 技術分享 需要 ber 排序。 Description Georgia and Bob decide to play a self-invented game. They draw a row of grids on pape