C. Robot

Time Limit: 3000ms
Case Time Limit: 3000ms
Memory Limit: 262144KB
64-bit integer IO format: %lld      Java class name: Main
Font Size: 
+
 
-

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

The first line contains an integer T (1 ≤ T ≤ 10) -- the number of test cases.



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

For each test case, output one integer in a single line -- the minimal number of released robots.

Sample Input

2
3 3
AA.
.A.
.AA
5 8
AAB.....
.ABBB...
.AAAAA..
..BBBAAB
.....AAA

Sample Output

1
2
Submit 

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)的更多相关文章

  1. Cleaning Robot (bfs+dfs)

    Cleaning Robot (bfs+dfs) Here, we want to solve path planning for a mobile robot cleaning a rectangu ...

  2. 深搜(DFS)广搜(BFS)详解

    图的深搜与广搜 一.介绍: p { margin-bottom: 0.25cm; direction: ltr; line-height: 120%; text-align: justify; orp ...

  3. 【算法导论】图的广度优先搜索遍历(BFS)

    图的存储方法:邻接矩阵.邻接表 例如:有一个图如下所示(该图也作为程序的实例): 则上图用邻接矩阵可以表示为: 用邻接表可以表示如下: 邻接矩阵可以很容易的用二维数组表示,下面主要看看怎样构成邻接表: ...

  4. 深度优先搜索(DFS)与广度优先搜索(BFS)的Java实现

    1.基础部分 在图中实现最基本的操作之一就是搜索从一个指定顶点可以到达哪些顶点,比如从武汉出发的高铁可以到达哪些城市,一些城市可以直达,一些城市不能直达.现在有一份全国高铁模拟图,要从某个城市(顶点) ...

  5. 【BZOJ5492】[HNOI2019]校园旅行(bfs)

    [HNOI2019]校园旅行(bfs) 题面 洛谷 题解 首先考虑暴力做法怎么做. 把所有可行的二元组全部丢进队列里,每次两个点分别向两侧拓展一个同色点,然后更新可行的情况. 这样子的复杂度是\(O( ...

  6. 深度优先搜索(DFS)和广度优先搜索(BFS)

    深度优先搜索(DFS) 广度优先搜索(BFS) 1.介绍 广度优先搜索(BFS)是图的另一种遍历方式,与DFS相对,是以广度优先进行搜索.简言之就是先访问图的顶点,然后广度优先访问其邻接点,然后再依次 ...

  7. 图的 储存 深度优先(DFS)广度优先(BFS)遍历

    图遍历的概念: 从图中某顶点出发访遍图中每个顶点,且每个顶点仅访问一次,此过程称为图的遍历(Traversing Graph).图的遍历算法是求解图的连通性问题.拓扑排序和求关键路径等算法的基础.图的 ...

  8. 数据结构与算法之PHP用邻接表、邻接矩阵实现图的广度优先遍历(BFS)

    一.基本思想 1)从图中的某个顶点V出发访问并记录: 2)依次访问V的所有邻接顶点: 3)分别从这些邻接点出发,依次访问它们的未被访问过的邻接点,直到图中所有已被访问过的顶点的邻接点都被访问到. 4) ...

  9. 层层递进——宽度优先搜索(BFS)

    问题引入 我们接着上次“解救小哈”的问题继续探索,不过这次是用宽度优先搜索(BFS). 注:问题来源可以点击这里 http://www.cnblogs.com/OctoptusLian/p/74296 ...

随机推荐

  1. [MVC4-基礎] 連動DropDownList - 使用jQuery、JSON

    剛開始學MVC4,以下是一些基礎的學習筆記! 先展示一下結果: 1.選擇申請部門 2.選好後申請部門鎖住防止USER修改並載入該部門所擁有的設備類型 一.資料庫 dept mf_fx 二.View ( ...

  2. CSS样式之背景、文本

    一.背景     1.背景颜色用background-color属性,例如:body{background-color:red}     2.用图像做背景用background-image属性,例如b ...

  3. Android与JS混编(js调用android相机扫描二维码)

    参考demo http://www.cnblogs.com/mythou/p/3280023.html        项目源码: https://github.com/weifengzz/Androi ...

  4. OpenGL ES 2.0 摄像机与投影

    1.摄像机的设置 摄像机的位置坐标 摄像机的位置 摄像机up方向 Matrix.setLookAtM( mVMatrix, //存储生成矩阵元素的float[]类型数组 0, //填充起始偏移量 cx ...

  5. BestCoder Round #36 (hdu5200)Strange Class(离线)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Trees Time Limit: 2000/1000 MS (Java/Othe ...

  6. jquery validate form 异步提交

    jQuery取得select选中的值 jQuery("#select1  option:selected").text(); 相信很多人都用过jquery validate插件,非 ...

  7. 转载:Struts2+Jquery实现ajax并返回json类型数据

    摘要: 主要实现步骤如下: 1.JSP页面使用脚本代码执行ajax请求 2.Action中查询出需要返回的数据,并转换为json类型模式数据 3.配置struts.xml文件 4.页面脚本接受并处理数 ...

  8. Symfony2 Doctrine从现有Database生成Entity(转载自http://blog.it985.com/6809.html)

    在我的以前一章Symfony之十分钟入门说了怎样生成数据库,然后设计实体Entity,再同步数据库的表结构,一般我们的顺序都是这样:生成数据库->设计实体Entity->同步数据库表结构. ...

  9. 可以ping通,浏览器打不开网页 - 解决办法

    网络故障表现为: 1.电脑显示网络连接正常,DNS配置和hosts配置均正常 2.cmd可以ping通网址,域名 3.所有浏览器无法打开网页,有道云笔记置灰,微信二维码刷新失败 解决办法: 管理员权限 ...

  10. android 退出方案 导致内存泄露

    比较奇怪android没有给出一个统一的退出接口,网上查了很多材料也出现了一些错误,在此记录一下,遇到的,与总结的. 1.常见概念,方法 finish() 通知结束当前activity实例,finis ...