11624 - Fire!
Posted: Thu Jul 30, 2009 6:04 pm
AC
Code: Select all
array[1103]
Code: Select all
else if(cha=='F')
{
array[y]=i;
array[y+1]=j;
y+=2;
Adjacent [ i ][ j ] = 1;
}
Code: Select all
5
1 1
J
1 2
J.
2 1
.
J
2 2
..
.J
2 2
..
FJ
Code: Select all
IMPOSSIBLE
2
2
2
2
Code: Select all
1
1
1
1
1
Code: Select all
#include <stdio.h>
#include <string.h>
#define inf 99999999
#define sz 1100
#define SZ 1100110
long r[]={0,0,-1,1},c[]={-1,1,0,0},dist_joe[sz][sz],dist_fire[sz][sz];
long cur_x,cur_y,que_x[SZ],que_y[SZ],head,tail;
long m,n,test,row,col,i,j,no_fire,joe_x,joe_y,cost;
char array[sz][sz];
void bfs_fire(int x,int y)
{
int i;
head = tail = 0; que_x[tail]=x; que_y[tail++]=y; dist_fire[x][y]=1;
while(head != tail)
{
cur_x=que_x[head]; cur_y=que_y[head++];
for(i=0;i<4;++i)
{
m=cur_x+r[i]; n=cur_y+c[i];
if(m<0 || n<0 || m>=row || n>=col)
continue;
if( array[m][n]=='.' && dist_fire[cur_x][cur_y]+1 < dist_fire[m][n])
{
dist_fire[m][n]=dist_fire[cur_x][cur_y]+1;
que_x[tail]=m; que_y[tail++]=n;
}
}
}
}
void bfs_joe(int x,int y)
{
int i;
head = tail = 0; que_x[tail]=x; que_y[tail++]=y; dist_joe[x][y]=1;
while(head != tail)
{
cur_x=que_x[head]; cur_y=que_y[head++];
for(i=0;i<4;++i)
{
m=cur_x+r[i]; n=cur_y+c[i];
if(m<0 || n<0 || m>=row || n>=col)
continue;
if( array[m][n]=='.' && dist_joe[cur_x][cur_y]+1 < dist_fire[m][n])
{
array[m][n]='#';
dist_joe[m][n]=dist_joe[cur_x][cur_y]+1;
que_x[tail]=m; que_y[tail++]=n;
}
}
}
}
int main()
{
scanf("%ld",&test);
while(test--)
{
scanf("%ld %ld",&row,&col);
for(i=0;i<row;++i)
scanf("%s",array[i]);
for(i=0;i<=row;i++)
for(j=0;j<=col;j++)
dist_fire[i][j]=dist_joe[i][j]=inf;
for(i=0;i<row;++i)
for(j=0;j<col;++j)
{
if(array[i][j]=='J')
joe_x=i , joe_y=j;
if(array[i][j]=='F')
bfs_fire(i,j);
}
bfs_joe(joe_x,joe_y);
cost=inf;
for(i=0;i<row;++i)
for(j=0;j<col;++j)
{
if((i==0 || j==0 || i==row-1 || j==col-1) && dist_joe[i][j] < cost )
cost=dist_joe[i][j];
}
if(cost==inf)
printf("IMPOSSIBLE\n");
else
printf("%ld\n",cost);
}
return 0;
}
thank u frnd i became foolish when i got accepted after changing data type int instead of long
Code: Select all
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#define MAX 1010
using namespace std;
int r,c;
char grid[MAX][MAX];
bool flag=false;
struct Node{
int x;
int y;
};
int dj[MAX][MAX];
int arr[]={-1,0,1,0,0,-1,0,1};
void bfs(Node s,Node y)
{
queue<Node> f;
queue <Node> j;
dj[s.x][s.y]=1;
j.push(s);
f.push(y);
int u,v;
while(!j.empty())
{
Node tmp=j.front();j.pop();
if(grid[tmp.x][tmp.y]!='F') //for J
{
for(int i=0;i<8;i+=2)
{
int row=tmp.x+arr[i];
int col=tmp.y+arr[i+1];
if(row>=0 && row<r && col>=0 && col<c )
{
if(grid[row][col]=='.')
{
Node t; t.x=row; t.y=col;
j.push(t);
dj[row][col]=dj[tmp.x][tmp.y]+1;
grid[row][col]='J';
}
}
else //if in last or first row or column
{
//cout<<tmp.x<<" "<<tmp.y<<" "<<dj[tmp.x][tmp.y]<<endl;
cout<<dj[tmp.x][tmp.y]<<endl;
flag=true; return;
u=tmp.x;
v=tmp.y;
}
}
}
if(!j.empty()) //for fire
{
tmp=f.front();f.pop();
for(int i=0;i<8;i+=2)
{
int row=tmp.x+arr[i];
int col=tmp.y+arr[i+1];
if(row>=0 && row<r && col>=0 && col<c )
{
if(grid[row][col]=='.'|| grid[row][col]=='J')
{
Node t; t.x=row;
t.y=col; f.push(t);
grid[row][col]='F';
}
}
}
}
}
}
int main()
{
int t;
scanf("%d",&t);
for(int m=0;m<t;m++)
{
scanf("%d%d",&r,&c);
for(int i=0;i<r;i++)
scanf("%s",grid[i]);
Node s,y;
s.x=-1;
for(int i=0;i<r;i++)
for(int j=0;j<c;j++)
{
if(grid[i][j]=='J')
s.x=i,s.y=j;
else if(grid[i][j]=='F')
y.x=i,y.y=j;
}
flag=false;
if(s.x!=-1)
bfs(s,y);
if(!flag)
printf("IMPOSSIBLE\n");
}
return 0;
}
Code: Select all
4
10 10
##########
#.....JFF#
#........#
#........#
#........#
#........#
#........#
#........#
#........#
..########
3 1
#
J
F
1 2
.J
5 5
#####
#..F#
#.J.#
....F
#####
Code: Select all
14
1
1
4
Code: Select all
#include<iostream>
#include<stdio.h>
#include<string>
#include<vector>
#include<algorithm>
#include<queue>
using namespace std;
int mani(int a,int b){
return a<b?a:b;
}
struct node{
int u,v;
}temp,joe;
vector<node>fires;
int n,m;
string grid[1010];
int movex[]={0,0,1,-1};
int movey[]={1,-1,0,0};
bool visit[1010][1010];
int len[1010][1010],len1[1010][1010];
queue<node>q;
void BFS_FIRE(node start,bool hat){
// cout<<"okp"<<endl;
for(int j=0;j<n;j++){
for(int k=0;k<m;k++)visit[j][k]=false;
}
visit[start.u][start.v]=true;
if(hat==true)len[start.u][start.v]=0;
else len1[start.u][start.v]=0;
q.push(start);
while(!q.empty()){
temp=q.front();
q.pop();
for(int i=0;i<4;i++){
node lol;
lol.u=temp.u+movex[i];
lol.v=temp.v+movey[i];
if(hat==true){
if(grid[lol.u][lol.v]!='#'&&visit[lol.u][lol.v]==false&&len[lol.u][lol.v]>len[temp.u][temp.v]+1&&(lol.u>=0&&lol.u<n)&&(lol.v>=0&&lol.v<m)){
visit[lol.u][lol.v]=true;
len[lol.u][lol.v]=len[temp.u][temp.v]+1;
q.push(lol);
}
}
else {
if(grid[lol.u][lol.v]!='#'&&visit[lol.u][lol.v]==false&&(lol.u>=0&&lol.u<n)&&(lol.v>=0&&lol.v<m)){
visit[lol.u][lol.v]=true;
len1[lol.u][lol.v]=len1[temp.u][temp.v]+1;
q.push(lol);
}
}
}
}
}
int main()
{
freopen("sam.txt","r",stdin);
int i,t,j,k;
cin>>t;
for(i=0;i<t;i++){
cin>>n>>m;
for(j=0;j<n;j++)cin>>grid[j];
for(j=0;j<n;j++){
for(k=0;k<m;k++){
if(grid[j][k]=='J'){
joe.u=j;
joe.v=k;
}
if(grid[j][k]=='F'){
temp.u=j;
temp.v=k;
fires.push_back(temp);
}
}
}
for(j=0;j<n;j++){
for(k=0;k<m;k++)len[j][k]=len1[j][k]=1000;
}
for(j=0;j<fires.size();j++)BFS_FIRE(fires[j],true);
BFS_FIRE(joe,false);
int mini=1000;
for(j=0;j<m;j++){
if(len1[0][j]<len[0][j])mini=mani(mini,len1[0][j]);
}
for(j=0;j<m;j++){
if(len1[n-1][j]<len[n-1][j])mini=mani(mini,len1[n-1][j]);
}
for(j=0;j<n;j++){
if(len1[j][0]<len[j][0])mini=mani(mini,len1[j][0]);
}
for(j=0;j<n;j++){
if(len1[j][m-1]<len[j][m-1])mini=mani(mini,len1[j][m-1]);
}
if(mini==1000)cout<<"IMPOSSIBLE"<<endl;
else cout<<mini+1<<endl;
fires.clear();
}
return 0;
}
Code: Select all
removed after AC.