Algorithm/Cases-Study

SWEA-1953 탈주범 검거

Xi_kor 2021. 3. 12. 22:15

딱히 방법이랄게 없다. 모든 방법과 경우를 고려해주었다.

checking() 함수는 현재 향하는 방향에서 다음 위치의 파이프가 연결이 되는지 여부를 확인해주었다.

direction 벡터는 현재 파이프의 번호를 받고 향할 수 있는 방향들을 모두 push해서 return 해주었다.

그렇게 받은 방향들을 토대로 BFS로 진행하였다.

마지막에 점검은 파이프가 존재하는 칸이면서, 방문한 칸이고, l값(탈출 후 소요된 시간)보다 작은 값일 경우에 방문했다고 가정하고 값을 하나씩 더해주었다. 

#include<iostream>
#include <algorithm>
#include <vector>
#include <cstring>
#include <queue>
using namespace std;

int dx[]={0, 1, 0, -1};
int dy[]={1, 0, -1, 0};

int ch[60][60];
int arr[60][60];
int n, m, r, c, l;

bool checking(int dir, int x, int y){
	if(dir==0){
		if(arr[x][y]==1||arr[x][y]==3||arr[x][y]==6||arr[x][y]==7) return true;
		else return false;
	}
	else if(dir==1){
		if(arr[x][y]==1||arr[x][y]==2||arr[x][y]==4||arr[x][y]==7) return true;
		else return false;
	}
	else if(dir==2){
		if(arr[x][y]==1||arr[x][y]==3||arr[x][y]==4||arr[x][y]==5) return true;
		else return false;
	}
	else{
		if(arr[x][y]==1||arr[x][y]==2||arr[x][y]==5||arr[x][y]==6) return true;
		else return false;
	}
}

vector <int> direction(int x){
	vector <int> temp;
	if(x==1){
		temp.push_back(0);
		temp.push_back(1);
		temp.push_back(2);
		temp.push_back(3);
	}
	else if(x==2){
		temp.push_back(1);
		temp.push_back(3);
	}
	else if(x==3){
		temp.push_back(0);
		temp.push_back(2);
	}
	else if(x==4){
		temp.push_back(0);
		temp.push_back(3);
	}
	else if(x==5){
		temp.push_back(0);
		temp.push_back(1);
	}
	else if(x==6){
		temp.push_back(1);
		temp.push_back(2);
	}
	else if(x==7){
		temp.push_back(2);
		temp.push_back(3);
	}
	return temp;
}

int main(int argc, char** argv)
{
	int test_case;
	int T;
	cin>>T;
	for(test_case = 1; test_case <= T; ++test_case)
	{
		memset(ch, 0, sizeof(ch));
		memset(arr, 0, sizeof(arr));
		cin>>n>>m>>r>>c>>l;
	
		for(int i=0; i<n; i++){
			for(int j=0; j<m; j++){
				cin>>arr[i][j];
			}
		}
		
		queue<pair<int, int> > q;
		q.push(make_pair(r, c));
		ch[r][c]=1;
	
		while(!q.empty()){
			int x, y;
			x=q.front().first;
			y=q.front().second;
			q.pop();
		
			vector <int> d=direction(arr[x][y]);
		
			for(int i=0; i<d.size(); i++){
				int nx=x+dx[d[i]];
				int ny=y+dy[d[i]];
			
				if(arr[nx][ny]!=0&&ch[nx][ny]==0&&checking(d[i], nx, ny)){
					ch[nx][ny]=ch[x][y]+1;
					q.push(make_pair(nx, ny));
				}
			}
			d.clear();
		}
		int cnt=0;
		for(int i=0; i<n; i++){
			for(int j=0; j<m; j++){
				if(ch[i][j]>0&&ch[i][j]<=l&&arr[i][j]!=0)	cnt++;
			}
		}
		cout<<"#"<<test_case<<" "<<cnt<<endl;
	}	
	return 0;//정상종료시 반드시 0을 리턴해야합니다.
}