관리 메뉴

너와 나의 스토리

[SW Expert Academy] 1824. 혁진이의 프로그램 검증 본문

Algorithm/구현

[SW Expert Academy] 1824. 혁진이의 프로그램 검증

노는게제일좋아! 2020. 6. 7. 01:04
반응형

문제: https://swexpertacademy.com/main/solvingProblem/solvingProblem.do

 

 

소스 코드:

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <queue>
#include <vector>
#include <string.h>
#include <algorithm>
#include <deque>
#include <cmath>
#include <string>
#include <stdio.h>
#include <set>
#include <cstdlib>
using namespace std;

int tc, n, m;
int dx[4] = { -1,0,1,0 };
int dy[4] = { 0,1,0,-1 };
char arr[22][22];
bool visit[22][22][4][17];


struct state {
	int curx;
	int cury;
	int dir;
	int memory;
};

queue<state> q;
bool func() {
	while (!q.empty()) q.pop();
	q.push({ 0, 0, 1, 0 });

	while (!q.empty()) {
		int curx = q.front().curx;
		int cury = q.front().cury;
		int dir = q.front().dir;
		int memory = q.front().memory;
		q.pop();
		if (visit[curx][cury][dir][memory]) continue;
		visit[curx][cury][dir][memory]=true;

		if (arr[curx][cury] == '<') {
			dir = 3;
		}
		else if (arr[curx][cury] == '>') {
			dir = 1;
		}
		else if (arr[curx][cury] == '^') {
			dir = 0;
		}
		else if (arr[curx][cury] == 'v') {
			dir = 2;
		}
		else if (arr[curx][cury] == '_') {
			if (memory == 0) {
				//if (visit[curx][cury]==5|| visit[curx][cury] ==12) return false;
				dir = 1;
			}
			else {
				//if (visit[curx][cury] == 7 || visit[curx][cury] == 12) return false;
				dir = 3;
			}
		}
		else if (arr[curx][cury] == '|') {
			if (memory == 0) {
				//if (visit[curx][cury] == 6 || visit[curx][cury] == 10) return false;
				dir = 2;
			}
			else {
				//if (visit[curx][cury] == 4|| visit[curx][cury] == 10) return false;
				dir = 0;
			}
		}
		else if (arr[curx][cury] == '?') {
			for (int i = 0; i < 4; i++) {
				int x = (curx + dx[i] + n) % n;
				int y = (cury + dy[i] + m) % m;
				q.push({ x,y,i,memory });
			}
			continue;
		}
		else if (arr[curx][cury] == '@') return true;
		else if (arr[curx][cury] == '+') {
			memory = (memory + 1) % 16;
		}
		else if (arr[curx][cury] == '-') {
			memory = (memory - 1+16) % 16;
		}
		else if (arr[curx][cury] == '.') {
			
		}
		else {
			memory = arr[curx][cury] - '0';
		}
		curx = (curx + dx[dir]+n) % n;
		cury = (cury + dy[dir] + m) % m;
		q.push({ curx,cury,dir,memory });
	}
	
	
	return false;
}

int main() {
	cin.tie(0), cout.tie(0);
	ios::sync_with_stdio(false);


	cin >> tc;
	for (int z = 1; z <= tc; z++) {
		cin >> n >> m;
		memset(visit, 0, sizeof(visit));
		
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				cin >> arr[i][j];
			}
		}
		if (func()) {
			cout << "#" << z << " YES\n";
		}
		else {
			cout << "#" << z << " NO\n";
		}
	}
	
	return 0;
}

 

반응형
Comments