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 ...
随机推荐
- JS基础函数
函数是由事件驱动的或者当它被调用时执行的可重复使用的代码块 <script> function demo(){ var a = 10; var b = 25; var sum = a + ...
- js-浏览器DOM
1.window尺寸 适配所有浏览器的尺寸方案 var w=window.innerWidth|| document.documentElement.clientWidth|| document.bo ...
- iOS RGB颜色封装
使用类别创建 .h文件 #import <UIKit/UIKit.h> @interface UIColor (HexColor) + (UIColor *)colorWithHex:(N ...
- iOS的触摸事件
在iOS中不是任何对象都能处理事件,只有继承了UIResponder的对象才能接收并处理事件,我们称其为@''响应者对象''UIApplication,UIViewController,UIView都 ...
- ANSI与UINCODE编码
简要说明: ANSI是一种字符代码,为使计算机支持更多语言,通常使用 0x80~0xFF 范围的 2 个字节来表示 1 个字符. Uincode(统一码.万国码.单一码)是计算机科学领域里的一项业界标 ...
- c++ 编译期计算 (一)
编译期就是编译器进行编译,产生.obj文件的所处的那一段时间(如果是广义的编译期,那么一般还包括了链接期,因为现在很多编译器都会自动调用链接器进行链接)执行期就是你执行某个已经链接好的程序的那段时间. ...
- hdu5358 First One(尺取法)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud First One Time Limit: 4000/2000 MS (Java/ ...
- HTML5画布(矩形)
canvas元素用于绘制图形. canvas元素是HTML5中新增的一个重要元素,元素本身是没有绘图能力,所有的绘制工作必须在javascript内部完成. 案例1: <!DOCTYPE htm ...
- 一个好玩的jq+php实现转盘抽奖程序
前台页面: <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <met ...
- 换行word-wrap与word-break兼容IE和FIREFOX -----设计师零张
word-wrap是控制换行的.使用break-word时,是将强制换行.中文没有任何问题,英文语句也没问题.但是对于长串的英文,就不起作用.word-break是控制是否断词的.normal是默认情 ...