zoukankan      html  css  js  c++  java
  • Red and Black(红与黑)BFS

    题目大意:

    从 ‘  @  ’ 出发,只能走‘   .  ’ ,不能走‘ # ’,只能上下左右移动,最终可以走多少步,注意的是,@也算一步。

    F - Red and Black

    Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:30000KB     64bit 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)
    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

    45
    59
    6
    13
    
    #include<iostream>
    #include<cstring>
    #include<queue>
    #include<algorithm>
    using namespace std;
    
    int x,y,ans,m,n;
    char map[22][22];//地图 
    bool vis[22][22];//表示访问过没有 
    int move[4][2] = {0,1,0,-1,1,0,-1,0};//移动 其中a[i][0]表示X移动, a[i][1]表示y移动.
    struct AC
    {
    	int x,y;
    }node,temp;
    
    bool judge (int x,int y)
    {
    	if (x>=0 && x<m && y>=0 && y<n )//判断坐标是否越界 
    	return true;
    	else
    	return false;
    }
    
    int bfs(int x,int y)
    {
    	ans=0;记录答案 
    	queue<AC>q;
    	node.x = x;
    	node.y = y;
    	
    	q.push(node);
    	
    	while (!q.empty())//模板写法 
    	{
    		AC top = q.front();
    		q.pop();
    		
    		for (int i=0;i<4;i++)
    		{
    			temp.x = top.x + move[i][0];//分别上下左右走动 
    			temp.y = top.y + move[i][1];
    			if (judge(temp.x,temp.y)==true && vis[temp.x][temp.y]==false && map[temp.x][temp.y]=='.')
    			{
    				ans++;		//能走的话就加一下 
    				q.push(temp);
    				vis[temp.x][temp.y] = true;
    			}
    		}
    	}
    	return ans;
    }
    
    
    int main()
    {
    
    	while (cin>>n>>m)
    	{
    		ans=0;
    		if (n==0 && m==0) break;
    		memset(vis,0,sizeof(vis));
    		
    		getchar();
    		for (int i=0;i<m;i++)
    		scanf("%s",map[i]);
    		
    		for (int i=0;i<m;i++)
    		{
    			for (int j=0;j<n;j++)
    			{
    				if (map[i][j]=='@')//找到@的位置,作为bfs的参数 
    				{
    					x=i;
    					y=j;
    					break;
    				}
    			}
    		}
    		cout<<bfs(x,y)+1<<endl;//最终答案要加 1  
    	}
    	
    	return 0;
    }
    //最简单的bfs模板题了,只要背了模板就能做出来 
  • 相关阅读:
    LInux常用命令:总结
    SpringBoot声明式事务(转)
    连接linux客户端工具
    查看servlet 3.0文档方法
    通过spring.io找spring历史版本
    归并排序(比希尔还要快)
    快速排序(比希尔排序还要快)
    希尔排序(交换式和移位式)
    插入排序
    选择排序(时间复杂度O(n^2))
  • 原文地址:https://www.cnblogs.com/Romantic-Chopin/p/12451469.html
Copyright © 2011-2022 走看看