UVa 10653 - Bombs! NO they are Mines!!
#include<cstdio>
#include<queue>
#include<cstring>
#include<cmath>
using namespace std;
// Accepted
#define sf scanf
#define pf printf
const int high = 2003;
int r , c , adj[high][high] , cost[high][high];
int dir_x[4] = {1 , -1 , 0 , 0} , dir_y[4] = {0 , 0 , 1 , -1}; // Consider it as 4 direction co-ordinate
bool visited[high][high];
void BFS(int x , int y)
{
queue<int>Q;
Q.push(x);
Q.push(y);
adj[x][y] = -1;
visited[x][y] = true;
cost[x][y] = 0;
while(!Q.empty())
{
int ux = Q.front();
Q.pop();
int uy = Q.front();
Q.pop();
for(int i=0; i<4; i++)
{
int vx = ux + dir_x[i];
int vy = uy + dir_y[i];
if((vx >= 0 && vx <= r) && (vy >= 0 && vy <= c) && (adj[vx][vy] == 0))
{
if(!visited[vx][vy] && !cost[vx][vy])
{
visited[vx][vy] = true;
cost[vx][vy] = cost[ux][uy] + 1;
Q.push(vx);
Q.push(vy);
}
}
}
}
}
int main()
{
int n , tr , tc , tn;
while(~sf("%d %d", &r , &c))
{
if(!r && !c) break;
memset(adj , 0 , sizeof adj);
memset(cost , 0 , sizeof cost);
memset(visited , false , sizeof visited);
sf("%d", &n);
for(int i=0; i<n; i++)
{
sf("%d %d" , &tr , &tn);
while(tn--)
{
sf("%d", &tc);
adj[tr][tc] = -1;
}
}
int sr , sc , er , ec;
sf("%d %d", &sr , &sc);
sf("%d %d", &er , &ec);
BFS(sr , sc);
pf("%d\n" , cost[er][ec]);
}
return 0;
}
#include<queue>
#include<cstring>
#include<cmath>
using namespace std;
// Accepted
#define sf scanf
#define pf printf
const int high = 2003;
int r , c , adj[high][high] , cost[high][high];
int dir_x[4] = {1 , -1 , 0 , 0} , dir_y[4] = {0 , 0 , 1 , -1}; // Consider it as 4 direction co-ordinate
bool visited[high][high];
void BFS(int x , int y)
{
queue<int>Q;
Q.push(x);
Q.push(y);
adj[x][y] = -1;
visited[x][y] = true;
cost[x][y] = 0;
while(!Q.empty())
{
int ux = Q.front();
Q.pop();
int uy = Q.front();
Q.pop();
for(int i=0; i<4; i++)
{
int vx = ux + dir_x[i];
int vy = uy + dir_y[i];
if((vx >= 0 && vx <= r) && (vy >= 0 && vy <= c) && (adj[vx][vy] == 0))
{
if(!visited[vx][vy] && !cost[vx][vy])
{
visited[vx][vy] = true;
cost[vx][vy] = cost[ux][uy] + 1;
Q.push(vx);
Q.push(vy);
}
}
}
}
}
int main()
{
int n , tr , tc , tn;
while(~sf("%d %d", &r , &c))
{
if(!r && !c) break;
memset(adj , 0 , sizeof adj);
memset(cost , 0 , sizeof cost);
memset(visited , false , sizeof visited);
sf("%d", &n);
for(int i=0; i<n; i++)
{
sf("%d %d" , &tr , &tn);
while(tn--)
{
sf("%d", &tc);
adj[tr][tc] = -1;
}
}
int sr , sc , er , ec;
sf("%d %d", &sr , &sc);
sf("%d %d", &er , &ec);
BFS(sr , sc);
pf("%d\n" , cost[er][ec]);
}
return 0;
}
Comments
Post a Comment