Codeforces #348 Div2 C
考察
問題を誤読していて,サンプルが合わなくて辛かった。
僕が行った解法としては愚直に与えらたクエリを実行して,指定された行,列の中の要素をシフトし,与えられた値を格納していく。
そして、すべてのクエリを実行し終わったら,逆からクエリをもう一度読み込んで,操作3の場合は何もせず,操作1の場合は右にシフト,操作2の場合は下にシフトしていく。
そうすると求めるべき解を得られる。
ソースコード
import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.NoSuchElementException; import java.util.*; public class Main { int N,M,Q; int[] t,r,c,x; int[][] table; public void shiftLeftRow(int n){ int tmp = table[n][0]; for(int i = 1;i < M;i++){ table[n][i - 1] = table[n][i]; } table[n][M - 1] = tmp; } public void shiftRightRow(int n){ int tmp = table[n][M - 1]; for(int i = M - 1;i >= 1;i--){ table[n][i] = table[n][i - 1]; } table[n][0] = tmp; } public void shiftTopColumn(int n){ int tmp = table[0][n]; for(int i = 1;i < N;i++){ table[i - 1][n] = table[i][n]; } table[N - 1][n] = tmp; } public void shiftBottomColumn(int n){ int tmp = table[N - 1][n]; for(int i = N - 1;i >= 1;i--){ table[i][n] = table[i - 1][n]; } table[0][n] = tmp; } public void solve(){ N = nextInt(); M = nextInt(); Q = nextInt(); table = new int[N][M]; t = new int[Q]; r = new int[Q]; c = new int[Q]; x = new int[Q]; for(int i= 0;i < Q;i++){ t[i] = nextInt(); if(t[i] == 1){ r[i] = nextInt() - 1; shiftLeftRow(r[i]); }else if(t[i] == 2){ c[i] = nextInt() - 1; shiftTopColumn(c[i]); }else{ r[i] = nextInt() - 1; c[i] = nextInt() - 1; x[i] = nextInt(); table[r[i]][c[i]] = x[i]; } } for(int i = Q - 1;i >= 0;i--){ if(t[i] == 3)continue; if(t[i] == 1){ shiftRightRow(r[i]); }else{ shiftBottomColumn(c[i]); } } for(int i = 0;i < N;i++){ for(int j = 0;j < M;j++){ if(j != 0)out.print(" "); out.print(table[i][j]); } out.println(); } } public static void main(String[] args) { out.flush(); new Main().solve(); out.close(); } /* Input */ private static final InputStream in = System.in; private static final PrintWriter out = new PrintWriter(System.out); private final byte[] buffer = new byte[2048]; private int p = 0; private int buflen = 0; private boolean hasNextByte() { if (p < buflen) return true; p = 0; try { buflen = in.read(buffer); } catch (IOException e) { e.printStackTrace(); } if (buflen <= 0) return false; return true; } public boolean hasNext() { while (hasNextByte() && !isPrint(buffer[p])) { p++; } return hasNextByte(); } private boolean isPrint(int ch) { if (ch >= '!' && ch <= '~') return true; return false; } private int nextByte() { if (!hasNextByte()) return -1; return buffer[p++]; } public String next() { if (!hasNext()) throw new NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = -1; while (isPrint((b = nextByte()))) { sb.appendCodePoint(b); } return sb.toString(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } }