C. Robot(BFS)

C. Robot

Time Limit: 3000ms

Case Time Limit: 3000ms

Memory Limit: 262144KB

64-bit integer IO format: %lld      Java class name: Main

Submit Status PID:
36000

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 Status 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);
    }
}
时间: 2024-10-06 03:07:57

C. Robot(BFS)的相关文章

1600 - Patrol Robot(BFS)

BFS题,不过多了一个很有意思的条件:不能连续穿越K个障碍,就好像多了一个技能一样,我用pre[][]数组来记录目前的k值: #include<bits/stdc++.h> using namespace std; int a[30][30],T,m,n,k,d[30][30],pre[30][30],air[10]={1,0,-1,0},air2[10]={0,1,0,-1};; typedef pair<int,int> P; P s[450]; int bfs() { que

pots(BFS)

D - Pots Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Submit Status Description You are given two pots, having the volume of A and B liters respectively. The following operations can be performed: Input On the first and

USACO抓牛catchcow (bfs)

这题是黄巨大出的比赛题. http://poj.org/problem?id=3278 Description Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and the cow is at a point K (0 ≤ K ≤

hdu 1728 逃离迷宫 (BFS)

逃离迷宫 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 14376    Accepted Submission(s): 3458 Problem Description 给定一个m × n (m行, n列)的迷宫,迷宫中有两个位置,gloria想从迷宫的一个位置走到另外一个位置,当然迷宫中有些地方是空地,gloria可以穿越,有些地方

牛汇(BFS)入金具体流程(图文指导)

牛汇开户流程:bfsforex牛汇入金教程 所谓入金,也就是充值的意思,必须充钱到平台才能进行外汇交易.首先,我们先登录bfsforex牛汇官方网站,在交易办公室功能区域下面,点击账户入金: 为您提供中国各大银行的网银支付解决方案,支持人民币支付,和信用卡入金,入金是实时到账的. 牛汇(BFS)入金具体流程(图文指导),布布扣,bubuko.com

URAL 1930 Ivan&#39;s Car(BFS)

Ivan's Car Time limit: 1.5 secondMemory limit: 64 MB The world is in danger! Awful earthquakes are detected all over the world. Houses are destroyed, rivers overflow the banks, it is almost impossible to move from one city to another. Some roads are

【判重+广搜(bfs)】魔板

判重+广搜(bfs)]魔板 Time Limit: 1000MS Memory Limit: 32768KB Special Judge 有一个两行四列的魔板,每个格子里有一个1到8的数字(数字唯一),现在我们可以对魔板进行以下操作: 1.交换两行的数字. 2.将第一列移到第二列,第二列到第三列,第三列到第四列,第四列到第一列. 3.将中间四个数顺时针转一次. 现给你初始状态,我末状态请你用最小的步数将它从初始状态变到末状态. 输入: 前两行,每行4个数表示初状态. 后两行,每行4个数表示末状态

[LeetCode] Binary Tree Zigzag Level Order Traversal(bfs)

Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its zig

USACO Mother&amp;#39;s Milk(bfs)

a=9MvljJDNdls&S=milk3">题目请点我 题解: 水杯倒水的问题非常经典,套路也是一样的,bfs找出全部状态. 这道题的关键在于每次都应该进行六次的倒水尝试,细心一点.PS:三维数组表示状态真的非常方便. 代码实现: /* ID: eashion LANG: C++ TASK: milk3 */ #include <iostream> #include <cstdio> #include <cstdlib> #include &l