단순 시뮬레이션

3048 단어
자신의 코드가 간결하지 않은 것 같아. 나중에 추출해야 돼...
 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char rsp[2][110][110];
int r, s, p;
void update(int k, int i, int j)
{
	if (i - 1 >= 0)
	{
		if (rsp[(k+1)%2][i][j] == 'R' && rsp[(k+1)%2][i - 1][j] == 'P')
		{
			rsp[k][i][j] = 'P';
		}
		if (rsp[(k+1)%2][i][j] == 'P' && rsp[(k+1)%2][i - 1][j] == 'S')
		{
			rsp[k][i][j] = 'S';
		}
		if (rsp[(k+1)%2][i][j] == 'S' && rsp[(k+1)%2][i - 1][j] == 'R')
		{
			rsp[k][i][j] = 'R';
		}
	}
	if (j - 1 >= 0)
	{
		if (rsp[(k+1)%2][i][j] == 'R' && rsp[(k+1)%2][i][j - 1] == 'P')
		{
			rsp[k][i][j] = 'P';
		}
		if (rsp[(k+1)%2][i][j] == 'P' && rsp[(k+1)%2][i][j - 1] == 'S')
		{
			rsp[k][i][j] = 'S';
		}
		if (rsp[(k+1)%2][i][j] == 'S' && rsp[(k+1)%2][i][j - 1] == 'R')
		{
			rsp[k][i][j] = 'R';
		}
	}
	if (i + 1 <= r)
	{
		if (rsp[(k+1)%2][i][j] == 'R' && rsp[(k+1)%2][i + 1][j] == 'P')
		{
			rsp[k][i][j] = 'P';
		}
		if (rsp[(k+1)%2][i][j] == 'P' && rsp[(k+1)%2][i + 1][j] == 'S')
		{
			rsp[k][i][j] = 'S';
		}
		if (rsp[(k+1)%2][i][j] == 'S' && rsp[(k+1)%2][i + 1][j] == 'R')
		{
			rsp[k][i][j] = 'R';
		}
	}
	if (j + 1 <= s)
	{
		if (rsp[(k+1)%2][i][j] == 'R' && rsp[(k+1)%2][i][j + 1] == 'P')
		{
			rsp[k][i][j] = 'P';
		}
		if (rsp[(k+1)%2][i][j] == 'P' && rsp[(k+1)%2][i][j + 1] == 'S')
		{
			rsp[k][i][j] = 'S';
		}
		if (rsp[(k+1)%2][i][j] == 'S' && rsp[(k+1)%2][i][j + 1] == 'R')
		{
			rsp[k][i][j] = 'R';
		}
	}
		
}
int main()
{
	int t;
	scanf("%d", &t);
	while (t --)
	{
		int k = 1;
		memset(rsp, 0 , sizeof(rsp));
		scanf("%d %d %d", &r, &s, &p);
		for (int i = 0; i < r; ++ i)
		{
			scanf("%s", rsp[0][i]);
		}
		for (int l = 0; l < p; ++ l)
		{
			for (int i = 0; i < r; ++ i)
			{
				for (int j = 0; j < s; ++ j)
				{
					rsp[k][i][j] = rsp[(k+1)%2][i][j];
					update(k, i, j);
				}
			}
			k = (k + 1) % 2;
		}
		k = (k + 1) % 2;
		for (int i = 0; i < r; ++ i)
		{
			printf("%s
", rsp[k][i]); } if (t != 0) { printf("
"); } } return 0; }

좋은 웹페이지 즐겨찾기