1. 程式人生 > >acm 2 1016 Red and Black

acm 2 1016 Red and Black

1.1016

2.

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
Sample Output 45 59 6 13

[email protected]是一個人的起始位置,#不可以走,. 可以走,求出可以走的個數

4.深度搜索,遞迴

5.

#include<iostream>
#include<string.h>
#include<vector>
#include<algorithm>
#include<numeric>
#include<string.h>
#include<sstream>
#include<stdio.h>
#include<string>
#include<cstdlib>
#include<queue>
#include<iomanip>
using namespace std;
int n,m;
char map[30][30];
struct ans
{
    int x;
    int y;
}n1,n2;
int ww(int a,int b)
{
    if(a>0&&a<=m&&b>0&&b<=n)
     return 1;
    return 0;
}
int BFS(int k,int z)
{
    int num=0,i;
    n1.x=k;
    n1.y=z;
    int disx[6]={-1,1,0,0};
    int disy[6]={0,0,-1,1};
    queue<ans> Q;
    Q.push(n1);
    while(!Q.empty())
    {
        n1=Q.front();
        Q.pop();
        for(i=0;i<4;i++)
        {
            n2.x=n1.x+disx[i];
            n2.y=n1.y+disy[i];
            if(ww(n2.x,n2.y)&&map[n2.x][n2.y]=='.')
            {
                num++;
                Q.push(n2);
                map[n2.x][n2.y]='#';
            }
        }
    }
    return num+1;
}


int main()
{
        int j,i,t,k,z;
    char x;
    while(cin>>n>>m&&n!=0||m!=0)
    {
        for(i=1;i<=m;i++)
        {
            for(j=1;j<=n;j++)
            {
                cin>>map[i][j];
                if(map[i][j]=='@') {k=i;z=j;}
            }
        }
        t=BFS(k,z);
        cout<<t<<endl;
    }
    return 0;
}

相關推薦

acm 2 1016 Red and Black

1.1016 2. Problem Description There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man i

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

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

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 (

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

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(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

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

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了. /*****************************************************************

POJ1979 Red and Black 走黑磚【DFS】

聯通塊 tro problem AC print 要求 pri 聯通 dir 題目鏈接 題目大意: 求圖中最大‘.‘的連通塊,輸出該最大連通塊的塊數 #include <cstdio> int n, m, res; int dir[4][2] = {

POJ 1979 Red and Black

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

Hdoj 1312.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 standing on a black tile. F

解題記錄:B - 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 black tile. From a til

HDU 1312 Red and Black(簡單BFS 聯通塊)

Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 26972&nbs

POJ1979 Red and Black

思路:先將圖存在二維陣列a[20][21]中,標記@的位置為x,y。從@處出發,不停的把鄰接部分用#代替,直到圖中不存在‘.’為止。 總共進行DFS次數就是答案了。4個方向對應四種狀態。 程式碼如下: #include<iostream> using namespace st

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

N - Red and Black ~~ [kuangbin帶你飛]專題一 簡單搜尋

有一個長方形的房間,覆蓋了正方形的磁磚。每塊磁磚的顏色,要麼是紅色,要麼是黑色。一名男子站在一塊黑色的磁磚上。他可以從一塊磁磚移至相鄰四塊磁磚中的某一塊。但是,他不允許在紅色磁磚上移動,他只允許在黑色磁磚上移動。 編寫一個程式,使得他允許重複上述的移動,判斷他所能到達的黑色磁磚的數量。 輸入

【POJ】1979 Red and Black(BFS)

Red and Black Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 44023 Accepted: 23850 Description There is a

POJ1979 Red and Black

思路:先將圖存在二維陣列a[20][21]中,標記@的位置為x,y。從@處出發,不停的把鄰接部分用#代替,直到圖中不存在‘.’為止。 總共進行DFS次數就是答案了。4個方向對應四種狀態。 程式碼如下: #include<iostream> using nam