7576번 - 토마토
#include<iostream>
using namespace std;
//토마토가 없는 곳 부터 토마토가 있는 곳 까지 거리 중 최대 값.
int x,y;
int map[1001][1001];
int count=0;
int day=0;
int visited[1001][1001];
int maxCount = 0;
int Q1x[1001];
int Q1y[1001];
int Q1count=0;
int Q1first=0;
int Q2x[1001];
int Q2y[1001];
int Q2count=0;
int Q2first=0;
int main()
{
cin>>y>>x;
int tomato;
int count = 0;
for(int i=1;i<=x;i++)
for( int j=1;j<=y;j++)
cin>>tomato;
map[i][j] = tomato;
if(tomato==1)
Q1x[Q1count] = i;
Q1y[Q1count] = j;
Q1count++;
count++;
maxCount++;
}
else if(tomato==0)
if(count==maxCount)
cout<<0;
return 0;
int day = 0;
while(count < maxCount )
if(Q1count==0)
break;
/*
cout<<"day"<<day<<endl<<endl;
for(int i=0;i<=x;i++)
for(int j=0;j<=y;j++)
cout<<map[i][j]<<" ";
cout<<endl;
cout<<"Qcount : "<<Q1count<<" count "<<count <<" maxCount"<<maxCount<<endl;
for(int i=0;i<Q1count;i++)
cout<<Q1x[i]<<" , "<<Q1y[i]<<endl;
*/
Q2first = 0;
Q2count = Q1count;
for(int i=0; i<Q1count;i++)
Q2x[i] = Q1x[i];
Q2y[i] = Q1y[i];
Q1count = 0;
day++;
int check = 0;
while(Q2count!=0)
int tempX = Q2x[Q2first];
int tempY = Q2y[Q2first];
//cout<<tempX<<"...."<<tempY<<endl;
visited[tempX][tempY] =1;
Q2first++;
Q2count--;
if( tempY+1<= y && map[tempX][tempY+1]==0 && visited[tempX][tempY+1]==0)
check++;
map[tempX][tempY+1]=1;
Q1x[Q1count] = tempX;
Q1y[Q1count] = tempY+1;
if( tempY-1>=1 && map[tempX][tempY-1]==0 && visited[tempX][tempY-1]==0)
map[tempX][tempY-1]=1;
Q1y[Q1count] = tempY-1;
if( tempX+1<=x && map[tempX+1][tempY]==0 && visited[tempX+1][tempY]==0)
map[tempX+1][tempY]=1;
Q1x[Q1count] = tempX+1;
Q1y[Q1count] = tempY;
if( tempX-1 >=1 && map[tempX-1][tempY]==0 && visited[tempX-1][tempY]==0)
map[tempX-1][tempY]=1;
Q1x[Q1count] = tempX-1;
if(count>=maxCount)
cout<<day<<endl;
else
cout<<-1;
소스는 소스코드란에 적어주세요. 이렇게 올리면 읽을 엄두가 안 납니다.
댓글을 작성하려면 로그인해야 합니다.
kmkoys 6년 전
#include<iostream>
using namespace std;
//토마토가 없는 곳 부터 토마토가 있는 곳 까지 거리 중 최대 값.
int x,y;
int map[1001][1001];
int count=0;
int day=0;
int visited[1001][1001];
int maxCount = 0;
int Q1x[1001];
int Q1y[1001];
int Q1count=0;
int Q1first=0;
int Q2x[1001];
int Q2y[1001];
int Q2count=0;
int Q2first=0;
int main()
{
cin>>y>>x;
int tomato;
int count = 0;
for(int i=1;i<=x;i++)
{
for( int j=1;j<=y;j++)
{
cin>>tomato;
map[i][j] = tomato;
if(tomato==1)
{
Q1x[Q1count] = i;
Q1y[Q1count] = j;
Q1count++;
count++;
maxCount++;
}
else if(tomato==0)
maxCount++;
}
}
if(count==maxCount)
{
cout<<0;
return 0;
}
int day = 0;
while(count < maxCount )
{
if(Q1count==0)
break;
/*
cout<<"day"<<day<<endl<<endl;
for(int i=0;i<=x;i++)
{
for(int j=0;j<=y;j++)
cout<<map[i][j]<<" ";
cout<<endl;
}
cout<<"Qcount : "<<Q1count<<" count "<<count <<" maxCount"<<maxCount<<endl;
for(int i=0;i<Q1count;i++)
{
cout<<Q1x[i]<<" , "<<Q1y[i]<<endl;
}
*/
Q2first = 0;
Q2count = Q1count;
for(int i=0; i<Q1count;i++)
{
Q2x[i] = Q1x[i];
Q2y[i] = Q1y[i];
}
Q1count = 0;
day++;
int check = 0;
while(Q2count!=0)
{
int tempX = Q2x[Q2first];
int tempY = Q2y[Q2first];
//cout<<tempX<<"...."<<tempY<<endl;
visited[tempX][tempY] =1;
Q2first++;
Q2count--;
if( tempY+1<= y && map[tempX][tempY+1]==0 && visited[tempX][tempY+1]==0)
{
check++;
map[tempX][tempY+1]=1;
count++;
Q1x[Q1count] = tempX;
Q1y[Q1count] = tempY+1;
Q1count++;
}
if( tempY-1>=1 && map[tempX][tempY-1]==0 && visited[tempX][tempY-1]==0)
{
check++;
map[tempX][tempY-1]=1;
count++;
Q1x[Q1count] = tempX;
Q1y[Q1count] = tempY-1;
Q1count++;
}
if( tempX+1<=x && map[tempX+1][tempY]==0 && visited[tempX+1][tempY]==0)
{
check++;
map[tempX+1][tempY]=1;
count++;
Q1x[Q1count] = tempX+1;
Q1y[Q1count] = tempY;
Q1count++;
}
if( tempX-1 >=1 && map[tempX-1][tempY]==0 && visited[tempX-1][tempY]==0)
{
check++;
map[tempX-1][tempY]=1;
count++;
Q1x[Q1count] = tempX-1;
Q1y[Q1count] = tempY;
Q1count++;
}
}
}
if(count>=maxCount)
cout<<day<<endl;
else
cout<<-1;
}