1 import java
.io
.FileReader
;
2 import java
.io
.IOException
;
3 import java
.io
.PrintWriter
;
4 import java
.util
.ArrayList
;
5 import java
.util
.Scanner
;
7 public class garbling_mb
implements Runnable
{
9 private PrintWriter out
;
11 private int[][] table
;
15 public static void main(String
[] args
) {
16 new Thread(new garbling_mb()).start();
21 in
= new Scanner(new FileReader("garbling.in"));
22 out
= new PrintWriter("garbling.out");
28 } catch (IOException e
) {
33 private void rotateClockwise(int row
, int col
) {
34 int a
= table
[row
][col
];
35 int b
= table
[row
][col
+ 1];
36 int c
= table
[row
+ 1][col
];
37 int d
= table
[row
+ 1][col
+ 1];
39 table
[row
][col
+ 1] = a
;
40 table
[row
+ 1][col
] = d
;
41 table
[row
+ 1][col
+ 1] = b
;
44 private void rotateCounterclockwise(int row
, int col
) {
45 int a
= table
[row
][col
];
46 int b
= table
[row
][col
+ 1];
47 int c
= table
[row
+ 1][col
];
48 int d
= table
[row
+ 1][col
+ 1];
50 table
[row
][col
+ 1] = d
;
51 table
[row
+ 1][col
] = a
;
52 table
[row
+ 1][col
+ 1] = c
;
55 private void applyMap(int row
, int col
) {
56 if (map
[row
][col
] == 'R') {
57 rotateClockwise(row
, col
);
58 } else if (map
[row
][col
] == 'L') {
59 rotateCounterclockwise(row
, col
);
63 private void solve() {
64 numRows
= in
.nextInt();
65 numCols
= in
.nextInt();
66 long numMoves
= in
.nextLong();
69 map
= new char[numRows
- 1][];
70 for (int row
= 0; row
< numRows
- 1; row
++) {
71 map
[row
] = new char[numCols
- 1];
72 final String line
= in
.nextLine();
73 for (int col
= 0; col
< numCols
- 1; col
++) {
74 map
[row
][col
] = line
.charAt(col
);
80 final long turnLength
= (numRows
- 1) * (numCols
- 1);
81 final long numTurns
= numMoves
/ turnLength
;
82 final int remMoves
= (int) (numMoves
% turnLength
);
84 long[] cycleDeltas
= new long[numRows
* numCols
];
85 simulate(turnLength
, cycleDeltas
);
88 long[] counters
= new long[numRows
* numCols
];
89 boolean[] used
= new boolean[numRows
* numCols
];
90 for (int i
= 0; i
< numRows
* numCols
; i
++) {
92 final ArrayList
<Integer
> permCycle
= new ArrayList
<Integer
>();
97 j
= table
[j
/ numCols
][j
% numCols
];
100 for (int k
= 0; k
< permCycle
.size(); k
++) {
101 final int from
= permCycle
.get(k
);
102 final int to
= permCycle
.get((int) ((k
+ numTurns
) % permCycle
.size()));
103 table
[from
/ numCols
][from
% numCols
] = to
;
107 for (int value
: permCycle
) {
108 totalDelta
+= cycleDeltas
[value
];
111 final long numCycles
= numTurns
/ permCycle
.size();
112 for (int value
: permCycle
) {
113 counters
[value
] += totalDelta
* numCycles
;
116 final int remCycles
= (int) (numTurns
% permCycle
.size());
117 for (int k
= 0; k
< remCycles
; k
++) {
118 for (int l
= 0; l
< permCycle
.size(); l
++) {
119 final int pos
= permCycle
.get(((l
+ k
) % permCycle
.size()));
120 counters
[pos
] += cycleDeltas
[permCycle
.get(l
)];
127 simulate(remMoves
, counters
);
129 for (long value
: counters
) {
134 private void simulate(long numMoves
, long[] counters
) {
137 for (int i
= 0; i
< numMoves
; i
++) {
138 counters
[table
[row
][col
]]++;
141 if (col
== numCols
- 1) {
145 if (row
== numRows
- 1) {
151 private void initTable() {
152 table
= new int[numRows
][];
154 for (int row
= 0; row
< numRows
; row
++) {
155 table
[row
] = new int[numCols
];
156 for (int col
= 0; col
< numCols
; col
++) {
157 table
[row
][col
] = counter
++;
162 private void dumpTable() {
163 for (int row
= 0; row
< numRows
; row
++) {
164 for (int col
= 0; col
< numCols
; col
++) {
165 System
.out
.printf("%3d ", table
[row
][col
]);
167 System
.out
.println();