1. 程式人生 > >poj 1979 搜尋

poj 1979 搜尋

Red and Black
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 19263 Accepted: 10259

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) 
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
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#[email protected]
#.#. .#.#####.#. .#.......#. .#########. ........... 11 6 ..#..#..#.. ..#..#..#.. ..#..#..### ..#..#..#@. ..#..#..#.. ..#..#..#.. 7 7 ..#.#.. ..#.#.. ###.### [email protected] ###.### ..#.#.. ..#.#.. 0 0

Sample Output

45
59
6
13
這是一道簡單的dfs,我手殘在忘記把檔案輸入的語句 給註釋了,結果就跪了。
下面是程式碼:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>

using namespace std;

int dir[8][2]={{-1,0},{0,1},{1,0},{0,-1}};
int w,h,ans;
char map[30][30];

void dfs(int x,int y){
     if(x<0||x>=h||y<0||y>=w)
         return;
      for(int i=0;i!=4;++i){
          int xx=x+dir[i][0];
          int yy=y+dir[i][1];
          if(map[xx][yy]=='.'){
              map[xx][yy]='1';
              dfs(xx,yy);
          }
      }
      return;
}

int main(){
     //freopen("input.txt","r",stdin);
    // freopen("output.txt","w",stdout);
     int sx,sy;
    while(scanf("%d%d",&w,&h)!=EOF&&w&&h){
         for(int i=0;i!=h;++i){
             scanf("%s",map[i]);
             for(int j=0;j!=w;++j){
                   if(map[i][j]=='@'){
                         sx=i;
                         sy=j;
                   }
             }
         }
         map[sx][sy]='1';
         ans=0;
         dfs(sx,sy);
         for(int i=0;i!=h;++i)
            for(int j=0;j!=w;++j){
                if(map[i][j]=='1')
                    ++ans;
            }
          cout<<ans<<endl;
    }
}


相關推薦

poj 1979 搜尋

Red and Black Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 19263 Accepted: 10259 Description There is a rectangular r

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

POJ 1979 POJ 3009 AOJ 0033 AOJ 0118 [搜索類題目][0033貪心模擬]

esp ear mes flag push cnblogs front tmp str /** POJ 1979 BFS */ #include <stdio.h> #include <string.h> #include <iostrea

POJ 1979 Red and Black (簡單dfs)

++ blog mage dir sin div names turn main 題目: 簡單dfs,沒什麽好說的 代碼: #include <iostream> using namespace std; typedef long long ll

POJ 1979 Red and Black (BFS)

搜索 ont eat size ++ mes 直接 pro eof 鏈接 : Here! 思路 : 簡單的搜索, 直接廣搜就ok了. /*****************************************************************

POJ 1979 Red and Black

水題 IT sin earch contain tput scanf sample eating Red and Black Time Limit: 1000MS Memory Limit: 30000K Total Submission

POJ 1979 Heavy Transportation (kruskal)

max i++ ret sport eth ecif cit scenario \n           Heavy Transportation Time Limit: 3000MS Memory Limit: 30000K Total Submissio

POJ 1979

原題目: Description There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is standing on a bla

ROADS(POJ) 剪枝+搜尋

題目描述 N個城市,編號1到N。城市間有R條單向道路。 每條道路連線兩個城市,有長度和過路費兩個屬性。 Bob只有K塊錢,他想從城市1走到城市N。問最短共需要走多長的路。如果到不了N,輸出-1 2<=N<=100 0<=K<=10000 1<=R<=1

Red and Black POJ - 1979

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 c

poj 3278 搜尋

描述: Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N

poj-1979

#include<stdio.h> #include<string.h> char map[25][25]; int n, m, num; int dr[4] = { -1,1,0,0 }; int dc[4] = { 0,0,1,-1 }; 

poj 3414 搜尋

Pots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8211 Accepted: 3481 Special Judge Description You are given two po

ACM:POJ-1979 Red And Black(JAVA的字元陣列輸入以及標記方法)

對於該題目直接用DFS或者BFS都可以直接暴力出來,實際上題目也不難,只需要遞迴一個搜尋方法即可。那麼用C或者C++將很容易的解決這個題目。 但是如果用JAVA寫的話將存在一個昨晚讓我糾結了很久的問題:輸入資訊需要以字元陣列的形式儲存,那麼如何解決標記以及儲存的問題? 剛開

poj 3050 搜尋

Hopscotch Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1462 Accepted: 1043 Description The cows play the child's game

poj 3414( 搜尋 )

#include <stdio.h> #include <string.h> #define N 101 struct Way { int prex; int prey; int kind; } way[N][N]; struct Point{ in

poj 2718 搜尋

Smallest Difference Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2679 Accepted: 766 Description Given a number of dis

poj 3278( 搜尋 )

Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 29250 Accepted: 9000 Description Farmer John has been in

poj 1321( 搜尋 )

棋盤問題 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14396 Accepted: 7100 Description 在一個給定形狀的棋盤(形狀可能是不規則的)上面擺放棋子,棋子沒有區

Coneology POJ - 2932(平面掃描+二叉搜尋樹)

傳送門 題意:平面上有N個兩兩都沒有公共點的圓,i號圓的圓心在(xi,yi),半徑為ri。求所有最外層的,即不包含於其他圓內部的圓。 題解: 附上程式碼: #include<iostream> #include<cstdio> #include<vect