C. Robot(BFS)
C. Robot
+
-
There is a rectangular field in the lab of Institution of Advanced Robotic Technology.
Many robots are about to release to this field one after another. They always enter the field from the upper left cell and leave from the lower right cell. In between, they can move vertically or horizontally to an adjacent cell on each step. At any time, there
is at most one robot on the field.
Robots can be divided into two types: Type A and Type B. During the movement, a robot will write down its type mark (A or B) in each cell on its track, which will cover all previous marks in the same cell. Notice that there is no mark on the field at the beginning.
Here is an example: initially there is no mark on the field (Figure 1); first, a robot of type A crosses the field from top-left to bottom right (Figure 2). After that, a robot of type B crosses and its tracks are partially covering the A’s (Figure 3).
..... AAAA. BBAA.
..... ..AA. .BBBB
..... ...A. ...AB
..... .AAA. .ABBB
..... ..AAA ..BBB
(1) (2) (3)
You are given the marks on the field after all robots have crossed. Write a program to determine the minimal possible number of released robots.
Input
For each test case:
The first line contains two integers h and w, indicates the height and width of the field. 1 ≤ h, w ≤ 4 000.
Then follows h lines, each line contains w characters: each character indicates the mark in the corresponding cell. A dot (“.”) indicates that there is no mark on this cell.
There is at least one mark on the field.
Output
Sample Input
2
3 3
AA.
.A.
.AA
5 8
AAB.....
.ABBB...
.AAAAA..
..BBBAAB
.....AAA
Sample Output
1
2
cid=5226#status/C" class="button ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" style="margin:0px 0.1em 0px -1px; padding:0px; text-decoration:none; font-family:'Trebuchet MS',Helvetica,Arial,sans-serif; font-size:1.1em; border:1px solid rgb(204,204,204); background-color:rgb(238,238,238); font-weight:bold; color:rgb(68,68,68); display:inline-block; position:relative; zoom:1; overflow:visible">Status
pid=36000" class="goprob button ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" style="margin:0px 0.1em 0px -1px; padding:0px; text-decoration:none; font-family:'Trebuchet MS',Helvetica,Arial,sans-serif; font-size:1.1em; border:1px solid rgb(204,204,204); background-color:rgb(238,238,238); font-weight:bold; color:rgb(68,68,68); display:inline-block; position:relative; zoom:1; overflow:visible">PID:
36000
#include<stdio.h>
#include<queue>
#include<iostream>
using namespace std;
typedef struct nnn
{
int x,y;
}node;
int vist[4005][4005];
char map[4005][4005];
int n,m,k,dir[4][2]={0,1,0,-1,1,0,-1,0},tf; void init()
{
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
vist[i][j]=0;
}
queue<node>q[2];
void bfs(int flag)
{
node p,s;
k++;
while(!q[flag].empty())
{
s=q[flag].front(); q[flag].pop();
if(s.x==0&&s.y==0)tf=1;
for(int e=0;e<4;e++)
{
p.x=s.x+dir[e][0]; p.y=s.y+dir[e][1];
if(p.x>=0&&p.x<n&&p.y>=0&&p.y<m&&vist[p.x][p.y]==0&&map[p.x][p.y]!='.')
{
vist[p.x][p.y]=1;
if(map[p.x][p.y]!=map[s.x][s.y])
q[!flag].push(p);
else q[flag].push(p);
}
}
}
}
int main()
{
int t,f,flag;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++)scanf("%s",map[i]);
k=0; tf=0;
while(!q[0].empty())q[0].pop();
while(!q[1].empty())q[1].pop(); if(map[0][0]!=map[n-1][m-1])f=1;else f=0;
if(map[n-1][m-1]!='.')
{
node s;
init();
s.x=n-1; s.y=m-1; flag=0;
vist[n-1][m-1]=1; q[0].push(s);
while(!q[flag].empty())
{
bfs(flag); flag=(!flag);
}
}
else f=0;
if(tf==0&&f)f=0;
printf("%d\n",k-f);
}
}
C. Robot(BFS)的更多相关文章
- Cleaning Robot (bfs+dfs)
Cleaning Robot (bfs+dfs) Here, we want to solve path planning for a mobile robot cleaning a rectangu ...
- 深搜(DFS)广搜(BFS)详解
图的深搜与广搜 一.介绍: p { margin-bottom: 0.25cm; direction: ltr; line-height: 120%; text-align: justify; orp ...
- 【算法导论】图的广度优先搜索遍历(BFS)
图的存储方法:邻接矩阵.邻接表 例如:有一个图如下所示(该图也作为程序的实例): 则上图用邻接矩阵可以表示为: 用邻接表可以表示如下: 邻接矩阵可以很容易的用二维数组表示,下面主要看看怎样构成邻接表: ...
- 深度优先搜索(DFS)与广度优先搜索(BFS)的Java实现
1.基础部分 在图中实现最基本的操作之一就是搜索从一个指定顶点可以到达哪些顶点,比如从武汉出发的高铁可以到达哪些城市,一些城市可以直达,一些城市不能直达.现在有一份全国高铁模拟图,要从某个城市(顶点) ...
- 【BZOJ5492】[HNOI2019]校园旅行(bfs)
[HNOI2019]校园旅行(bfs) 题面 洛谷 题解 首先考虑暴力做法怎么做. 把所有可行的二元组全部丢进队列里,每次两个点分别向两侧拓展一个同色点,然后更新可行的情况. 这样子的复杂度是\(O( ...
- 深度优先搜索(DFS)和广度优先搜索(BFS)
深度优先搜索(DFS) 广度优先搜索(BFS) 1.介绍 广度优先搜索(BFS)是图的另一种遍历方式,与DFS相对,是以广度优先进行搜索.简言之就是先访问图的顶点,然后广度优先访问其邻接点,然后再依次 ...
- 图的 储存 深度优先(DFS)广度优先(BFS)遍历
图遍历的概念: 从图中某顶点出发访遍图中每个顶点,且每个顶点仅访问一次,此过程称为图的遍历(Traversing Graph).图的遍历算法是求解图的连通性问题.拓扑排序和求关键路径等算法的基础.图的 ...
- 数据结构与算法之PHP用邻接表、邻接矩阵实现图的广度优先遍历(BFS)
一.基本思想 1)从图中的某个顶点V出发访问并记录: 2)依次访问V的所有邻接顶点: 3)分别从这些邻接点出发,依次访问它们的未被访问过的邻接点,直到图中所有已被访问过的顶点的邻接点都被访问到. 4) ...
- 层层递进——宽度优先搜索(BFS)
问题引入 我们接着上次“解救小哈”的问题继续探索,不过这次是用宽度优先搜索(BFS). 注:问题来源可以点击这里 http://www.cnblogs.com/OctoptusLian/p/74296 ...
随机推荐
- Win32多线程编程(3) — 线程同步与通信
一.线程间数据通信 系统从进程的地址空间中分配内存给线程栈使用.新线程与创建它的线程在相同的进程上下文中运行.因此,新线程可以访问进程内核对象的所有句柄.进程中的所有内存以及同一个进程中其他所有线 ...
- Tomcat项目部署方式
一.静态部署 1.直接将web项目文件件拷贝到webapps 目录中 Tomcat的Webapps目录是Tomcat默认的应用目录,当服务器启动时,会加载所有这个目录下的应用.所以可以将JS ...
- 一个cocos2d-x的项目
前几天完成了一个cocos2d-x的项目,放在git上: https://github.com/gittor/Jigsaw 采用cocos的版本是3.7.1. 项目是一个拼图的游戏,市面上的拼图类游戏 ...
- Angularjs 日期格式转换
我自己的随笔,记录我编码的点滴. <!DOCTYPE HTML><html><head> <meta charset="utf-8" ...
- HTML中元素水平居中。
一丶margin:0 auto; 试用最多的方法,简单实用. 二丶vertical-align:middle; 只适用于内嵌元素,比如说一个div中有一个图片和文字,要让图片和文字中线对齐. < ...
- 转载:s:if的用法
转载网址:http://blog.csdn.net/menhuanxiyou/article/details/5709550 1:直接写表达式 <s:set name="china&q ...
- php base64数据与图片的转换
1.解析base64数据成图片 The problem is that data:image/bmp;base64, is included in the encoded contents. This ...
- python学习第二天 --变量及其字符串
python变量: 在计算机程序中,变量不仅可以是数字,还可以是任意数据类型. 在Python程序中,变量是用一个变量名表示,变量名必须是大小写英文.数字和下划线(_)的组合,且不能用数字开头. 在P ...
- ubuntu vim YCM
http://blog.sina.com.cn/s/blog_499386b00100rxm1.html http://www.cnblogs.com/junnyfeng/p/3633697.html
- hdu 1596 find the safest road
http://acm.hdu.edu.cn/showproblem.php?pid=1596 #include <cstdio> #include <cstring> #inc ...