forked from s-kachroo/SamsungPractice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlaughing gas.cpp
70 lines (67 loc) · 1.5 KB
/
laughing gas.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//https://www.cnblogs.com/kingshow123/p/practicec1.html
#include <iostream>
#include <stdio.h>
#include <queue>
#include <string.h>
using namespace std;
typedef struct
{
int x;
int y;
int level;
}data;
int mv[4][2] = {{1,0},{0,1},{-1,0},{0,-1}};
//int mv[8][2] = {{-2,1},{-1,2},{1,2},{2,1},{2,-1},{1,-2},{-1,-2},{-2,-1}};
int main()
{
//freopen("test.txt","r",stdin);
int T;
cin>>T;
for(int t=1; t<=T; t++)
{
int n,m;
int r,c;
cin>>n>>m;
int a[m+1][n+1];
memset(a,0,sizeof(int)*m*n);
for(int i=1; i<=m; i++)
{
for(int j=1; j<=n; j++)
{
cin>>a[i][j];
}
}
cin>>r>>c;
data d,d1,d2;
queue<data> qt;
int tmx,tmy,tml;
d.x = c;
d.y = r;
d.level = 1;
qt.push(d);
a[d.x][d.y] = 2;
while(!qt.empty())
{
d1 = qt.front();
qt.pop();
for(int k=0; k<4; k++)
{
tmx = d1.x + mv[k][0];
tmy = d1.y + mv[k][1];
tml = d1.level + 1;
if(a[tmx][tmy] == 1)
{
d2.x = tmx;
d2.y = tmy;
d2.level = tml;
a[d2.x][d2.y] = 2;
qt.push(d2);
}
}
}
cout<<"Case #"<<t<<endl;
cout<<tml-1<<endl;
}
//cout << "Hello world!" << endl;
return 0;
}