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 ...
随机推荐
- Hadoop集群运行JNI程序
要在Hadoop集群运行上运行JNI程序,首先要在单机上调试程序直到可以正确运行JNI程序,之后移植到Hadoop集群就是水到渠成的事情. Hadoop运行程序的方式是通过jar包,所以我们需要将所有 ...
- Tomcat 原理篇
TOMCAT 原理篇一.Tomcat 组成(Tomcat 由以下组件组成) 1.server a) Server是一个Catalina Servlet容器: b) Server 可以包含一个或多个se ...
- Asp.net原理(第一篇)
Asp.net (第一篇) 当用户在浏览器输入一个URL地址后,浏览器会发送一个请求到服务器.这时候在服务器上第一个负责处理请求的是IIS.然后IIS再根据请求的URL扩展名将请求分发给不同的ISAP ...
- EF数据建模(一)
大中型软件开发过程中常会使用ORM技术,ORM全称是“对象-关系映射Object-Relation-Mappping”.是将数据库中的数据对象的形式表现出来,并将通过面向对象的方式将这些对象组织起来, ...
- 操作Action
Action就是Controller的一个方法 包含对应请求的最终结果 Action里代码从Model层拿到数据 最后把数据结果Actionresult 返回用户
- Windows命令行(DOS命令)教程-7 (转载)http://arch.pconline.com.cn//pcedu/rookie/basic/10111/15325_6.html
11. deltree [功能] 删除目录树 [格式] [C:][path]DELTREE [C1:][path1] [[C2:][path2] […]] [说明] 这个命令将整个指定目录树全部消灭, ...
- PL/SQL中字符串变量的分割转化
在编写PL/SQL时,有时候我们需要处理这样一个输入的变量,它的格式是由多个值通过分隔符组成的字符串,如“1,2,3”,我们需要将这个变量加入到我们的SQL中,形成诸如in('1','2','3')的 ...
- (二)CSS3应用 - 实现圆角
html <link href="test.css" type="text/css" rel="stylesheet" /> & ...
- 快速搭建Android 开发环境-使用ADT Bundle
一.搭建Android开发环境 近日要学Android开发基础,就着手搭建Windows下的Android开发环境. 找了一些相关的博文参考,基本上都是要分别下载和安装JDK, Eclipse, An ...
- python学习视频整理
python3英文视频教程(全87集) http://pan.baidu.com/s/1dDnGBvV python从入门到精通视频(全60集)链接:http://pan.baidu.com/s/1e ...