poj2339- 简单模拟

感觉自己的代码不够简洁啊,以后要提炼。。。

 

#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\n", rsp[k][i]);
		}
		if (t != 0)
		{
			printf("\n");
		}
	}
	return 0;
}


 

你可能感兴趣的:(poj2339- 简单模拟)