Little Elephant and Mouses

Little Elephant and Mouses

Problem code: LEMOUSE

It is well-known that the elephants are afraid of mouses. The Little Elephant from the Zoo of Lviv is not an exception.

The Little Elephant is on a board A of n rows andm columns (0-based numeration). At the beginning he is in cell with coordinates(0; 0) and he wants to go to cell with coordinates (n-1; m-1). From cell(x; y) Little Elephant can go either to (x+1; y) or(x; y+1).

Each cell of the board contains either 1 or 0. IfA[i][j] = 1, then there is a single mouse in cell (i; j). Mouse at cell(i; j) scared Little Elephants if and only if during the path there was at least one such cell(x; y) (which belongs to that path) and |i-x| + |j-y| <= 1.

Little Elephant wants to find some correct path from (0; 0) to(n-1; m-1) such that the number of mouses that have scared the Little Elephant is minimal possible. Print that number.

Input

First line contains single integer T - the number of test cases. ThenT test cases follow. First line of each test case contain pair of integersn and m - the size of the board. Next n lines containn strings, each of size m and consisted of digits0 and 1.

Output

In T lines print T integer - the answers for the corresponding test.

Constraints

1 <= T <= 50

2 <= n, m <= 100

Example

Input:
2
3 9
001000001
111111010
100100100
7 9
010101110
110110111
010011111
100100000
000010100
011011000
000100101

Output:
9
10

Explanation

Example case 1: The optimized path is: (0, 0) -> (0, 1) -> (0, 2) -> (0, 3) -> (0, 4) -> (0, 5) -> (0, 6) -> (0, 7) -> (0, 8) -> (1, 8) -> (2, 8). The mouses that scared the Little Elephant are at the following cells: (1, 0), (1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 7), (0, 2), (0, 8).

Example case 2: The optimized path is: (0, 0) -> (1, 0) -> (1, 1) -> (2, 1) -> (2, 2) -> (3, 2) -> (3, 3) -> (4, 3) -> (4, 4) -> (5, 4) -> (5, 5) -> (6, 5) -> (6, 6) -> (6, 7) -> (6, 8). The 10 mouses that scared the Little Elephant are at the following cells: (0, 1), (1, 0), (1, 1), (2, 1), (3, 3), (4, 4), (5, 4), (5, 5), (6, 6), (6, 8).


错误代码,嘎嘎!!

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<queue>
using namespace std ;
#define inf 1000000000
typedef struct infor
{
    int step ;
    int x ,  y ;
    int vi[103][103] ;
}infor;
char s[102][102];
int n , m , min1  , step[102][102] , move[2][2] = {{0,1},{1,0}};
bool inmap(int x ,int y )
{
    if(x>=0 && x <= n-1 && y >=0 && y<=m-1)
        return true ;
    else
        return false ;
}

int ismouse(int x , int y , infor c)
{
    int cnt = 0 ;
    if(s[x][y]=='1' && !c.vi[x][y]){
        cnt ++ ;
        c.vi[x][y] = 1;
    }
    if((s[x-1][y] == '1'&&x>=1) && !c.vi[x-1][y]){
        cnt ++ ;
        c.vi[x-1][y] = 1;
    }
    if((s[x][y-1] == '1'&&y>=1) && !c.vi[x][y-1]){
        cnt ++ ;
        c.vi[x][y-1] ;
    }
    if((x<n-1&&s[x+1][y]=='1') && !c.vi[x+1][y]){
        cnt ++;
        c.vi[x+1][y] = 1 ;
    }
    if((y<m-1&&s[x][y+1]=='1') && !c.vi[x][y+1]){
        cnt ++ ;
        c.vi[x][y+1] = 1 ;
    }
    return cnt ;
}

void BFS()
{
    infor a ;
    a.step = 0 ;
    a.x = 0 ;
    a.y = 0 ;
    memset(a.vi,0,sizeof(a.vi));
    queue<infor>q ;
    q.push(a) ;
    while(!q.empty())
    {
        infor b = q.front();
        memset(b.vi,0,sizeof(b.vi));
        q.pop();
        for( int i= 0 ; i < 2; i++)
        {
            infor c = b ;
            c.x +=move[i][0] ;
            c.y +=move[i][1] ;
            if(inmap(c.x,c.y) && step[c.x][c.y] > c.step)
            {

                c.step+=ismouse(c.x,c.y ,c );
                if(c.x == n-1 && c.y == m-1 )
                    min1 = min1 > c.step ? c.step : min1 ;
                q.push(c);
            }
        }
    }
}

int main(void)
{
    int t ;
    cin >> t ;
    while(t--)
    {
        cin >> n >> m ;
        int i ,  j ;
        min1 = inf ;
        for( i = 0 ;i < n ; i ++)
            for( j = 0 ; j < m ; j ++){
                cin >> s[i][j] ;
                step[i][j] = inf ;
            }
        step[0][0] = -1 ;
        BFS();
        cout<< min1 << endl;
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值