首页 > 专题系列 > Java解POJ > POJ 2339 Rock, Scissors, Paper [解题报告] Java
2013
11-11

POJ 2339 Rock, Scissors, Paper [解题报告] Java

Rock, Scissors, Paper

问题描述 :

Bart’s sister Lisa has created a new civilization on a two-dimensional grid. At the outset each grid location may be occupied by one of three life forms: Rocks, Scissors, or Papers. Each day, differing life forms occupying horizontally or vertically adjacent grid locations wage war. In each war, Rocks always defeat Scissors, Scissors always defeat Papers, and Papers always defeat Rocks. At the end of the day, the victor expands its territory to include the loser’s grid position. The loser vacates the position.

Your job is to determine the territory occupied by each life form after n days.

输入:

The first line of input contains t, the number of test cases. Each test case begins with three integers not greater than 100: r and c, the number of rows and columns in the grid, and n. The grid is represented by the r lines that follow, each with c characters. Each character in the grid is R, S, or P, indicating that it is occupied by Rocks, Scissors, or Papers respectively.

输出:

For each test case, print the grid as it appears at the end of the nth day. Leave an empty line between the output for successive test cases.

样例输入:

2
3 3 1
RRR
RSR
RRR
3 4 2
RSPR
SPRS
PRSP

样例输出:

RRR
RRR
RRR

RRRS
RRSP
RSPR

解题代码:

//* @author 洪晓鹏<[email protected]>
import java.util.Scanner;

public class Main {

	/**
	 * @param args
	 */
	public static int row;
	public static int column;

	public static int replace(int i, int j, int[][] array) {
		int temp = 0;
		if (array[i][j] == 'R')
			temp = 'P';
		else if (array[i][j] == 'S')
			temp = 'R';
		else if (array[i][j] == 'P')
			temp = 'S';

		if (i + 1 < row && array[i + 1][j] == temp)
			return temp;
		if (j + 1 < column && array[i][j + 1] == temp)
			return temp;
		if (i > 0 && array[i - 1][j] == temp)
			return temp;
		if (j > 0 && array[i][j - 1] == temp)
			return temp;
		return array[i][j];
	}

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
	
		for (int i = 0; i < n; i++) {
			row = in.nextInt();
			column = in.nextInt();
			int day = in.nextInt();
			String s = in.nextLine();
			int[][] array = new int[row][column];
			int[][] temp = new int[row][column];
			for (int j = 0; j < row; j++) {
			   String line = in.nextLine();
			   //System.out.println(line);
			   for (int k = 0; k < column; k++) {
				array[j][k] = line.charAt(k);
			   }
			}

		 for (int m = 0; m < day; m++) {
		  for (int j = 0; j < row; j++) {
			for (int k = 0; k < column; k++) {
				temp[j][k] = replace(j, k, array);
			}
		  }
		 int count = 0;
		 for (int j = 0; j < row; j++) {
			for (int k = 0; k < column; k++) {
				if (temp[j][k] == temp[0][0])
					count++;
				array[j][k] = temp[j][k];
			}
		}
		if (count == row * column) {
			break;
		}
	}
	for (int j = 0; j < row; j++) {
		// String line = in.nextLine();
		for (int k = 0; k < column; k++) {
			System.out.print((char) array[j][k]);
		}
			
			System.out.println();
	}
	if(i!=n-1)
		System.out.println();
     }

  }

}

  1. #include <stdio.h>
    int main()
    {
    int n,p,t[100]={1};
    for(int i=1;i<100;i++)
    t =i;
    while(scanf("%d",&n)&&n!=0){
    if(n==1)
    printf("Printing order for 1 pages:nSheet 1, front: Blank, 1n");
    else {
    if(n%4) p=n/4+1;
    else p=n/4;
    int q=4*p;
    printf("Printing order for %d pages:n",n);
    for(int i=0;i<p;i++){
    printf("Sheet %d, front: ",i+1);
    if(q>n) {printf("Blank, %dn",t[2*i+1]);}
    else {printf("%d, %dn",q,t[2*i+1]);}
    q–;//打印表前
    printf("Sheet %d, back : ",i+1);
    if(q>n) {printf("%d, Blankn",t[2*i+2]);}
    else {printf("%d, %dn",t[2*i+2],q);}
    q–;//打印表后
    }
    }
    }
    return 0;
    }

  2. 第一句可以忽略不计了吧。从第二句开始分析,说明这个花色下的所有牌都会在其它里面出现,那么还剩下♠️和♦️。第三句,可以排除2和7,因为在两种花色里有。现在是第四句,因为♠️还剩下多个,只有是♦️B才能知道答案。