zoukankan      html  css  js  c++  java
  • HDU 1312 Red and Black

    [color{blue}{Red;and;Black} ]

    [color{green}{Time;Limit: 2000/1000 MS (Java/Others)quad Memory;Limit: 65536/32768 K (Java/Others)} ]


    (color{CornflowerBlue}{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.


    (color{CornflowerBlue}{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)


    (color{CornflowerBlue}{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).


    (color{CornflowerBlue}{Sample;Input})

    6 9
    ....#.
    .....#
    ......
    ......
    ......
    ......
    ......
    #@...#
    .#..#.
    11 9
    .#.........
    .#.#######.
    .#.#.....#.
    .#.#.###.#.
    .#.#..@#.#.
    .#.#####.#.
    .#.......#.
    .#########.
    ...........
    11 6
    ..#..#..#..
    ..#..#..#..
    ..#..#..###
    ..#..#..#@.
    ..#..#..#..
    ..#..#..#..
    7 7
    ..#.#..
    ..#.#..
    ###.###
    ...@...
    ###.###
    ..#.#..
    ..#.#..
    0 0
    

    (color{CornflowerBlue}{Sample;Output})

    45
    59
    6
    13
    

    (color{CornflowerBlue}{Source})

    Asia 2004, Ehime (Japan), Japan Domestic


    (color{CornflowerBlue}{Recommend})

    Eddy | We have carefully selected several similar problems for you: 1372 1242 1253 1240 1072


    题目描述

    有一个长方形的房间,上面铺着方砖。 每个瓷砖都是红色或黑色的。 一个人站在黑色的瓷砖上。 从一个瓷砖,他可以移动到四个相邻的瓷砖之一。 但他不能在红色瓷砖上移动,他只能在黑色瓷砖上移动。

    编写一个程序来计算他可以通过重复上面描述的动作来达到的黑色瓷砖的数量。

    输入

    输入有多个组测试用例。 一组测试用例以包含两个正整数(W)(H)的行开头;(W)(H)分别是(x)方向和(y)方向的瓷砖数。 (W)(H)不超过(20)

    接下来有(H)行,每行包含(W)个字符。 每个字符表示瓷砖的颜色如下。

    ‘.’ -黑色瓷砖
    ‘#’-一块红色的瓷砖
    ‘@’-在黑色瓷砖上的一个人(在一组测试用例中出现一次)

    输出

    对于每组测试用例,您的程序应该输出一行,其中包含他可以从初始瓷砖(包括本身)到达的瓷砖数量。

    • PZ's solution:

    广搜模板题,不解释。

    • TAG:搜索;BFS广度优先搜索

    PZ.cpp

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<queue>
    using namespace std;
    int W,H,ans;
    int fx[]={0,0,1,-1};
    int fy[]={1,-1,0,0};
    char mp[25][25];
    bool vis[25][25];
    queue<int>qx,qy;
    bool in_map(int x,int y){ return 1<=x&&x<=H&&1<=y&&y<=W; }
    void bfs(int x,int y){
    	qx.push(x); qy.push(y); vis[x][y]=1; ++ans;
    	while(!qx.empty()){
    		x=qx.front(); qx.pop();
    		y=qy.front(); qy.pop();
    		for(int i=0;i<4;++i){
    			int nx=x+fx[i],ny=y+fy[i];
    			if(!in_map(nx,ny)||mp[nx][ny]=='#'||vis[nx][ny]) continue;
    			qx.push(nx); qy.push(ny); vis[nx][ny]=1; ++ans;
    		}
    	}
    }
    int main(){
    	while(scanf("%d %d",&W,&H)&&W!=0){
    		ans=0;
    		int sx,sy;
    		for(int i=1;i<=H;++i)
    			for(int j=1;j<=W;++j){
    				cin>>mp[i][j];
    				vis[i][j]=0;
    				if(mp[i][j]=='@') sx=i,sy=j;
    			}
    		bfs(sx,sy);
    		printf("%d
    ",ans);
    	}
    	return 0;
    }
    
  • 相关阅读:
    request.getParameter() 和request.getAttribute() 区别
    Java中this和super的用法总结
    jQuery实现列表框双向选择操作
    Eclipse中.setting目录下文件介绍
    通过Ajax方式上传文件,使用FormData进行Ajax请求
    ASP.NET程序中常用的三十三种代码
    计算地球上两个坐标点(经度,纬度)之间距离sql函数
    动态调用WebService(C#) (非常实用)
    带SoapHeader验证的WebServices
    GridView内容详解(转载)
  • 原文地址:https://www.cnblogs.com/Potrem/p/HDU_1312.html
Copyright © 2011-2022 走看看