题意:给出了三种矩阵加密方式,实现
思路:这三种方法加上之前的读数据处理我写伪代码用了一小时左右,有点恶心。。
技巧比较多,初学者可以好好研究一下这个题目的细节处理。不懂的可以给我留言,其实我的代码写的还是有点长了。
错点:忘记了00代表100
统计:212k, 0ms, 2Y
#include <iostream> #include <string> #include <ctype.h> #define F(i,a,b) for (int i=a;i<=b;i++) using namespace std; int movel[2][4][2] = { { {0, 1}, {1, 0}, {0, -1}, {-1, 0} }, { {1, 0}, {0, 1}, {-1, 0}, {0, -1} } }, size; char map[101][101]; void shake() { int move = 1, next; F(i,1,size) { int now = 1, temp = map[1][i]; F(j,1, size-1) { next = (now+move+size)%size; if (!next) next = size; map[now][i] = map[next][i]; now = next; } map[now][i] = temp; move*=-1; } } void rattle() { int move = -1, next; F(i, 1, size) { int now = 1, temp = map[i][1]; F(j,1,size-1) { next = (now+move+size)%size; if (!next) next = size; map[i][now] = map[i][next]; now = next; } map[i][now] = temp; move *= -1; } } void loop() { int nextx, nexty; F(i,1, size/2) { int dir = i%2, // dir: 0-counterclock, 1-clock temp = map[i][i], moving = 0, nowx = i, nowy = i; do { nextx = nowx+movel[dir][moving][0], nexty = nowy+movel[dir][moving][1]; if ( (nextx == i && nexty == size-i+1 ) || (nextx == size-i+1 && nexty == size-i+1 ) || (nextx == size-i+1 && nexty == i) ) moving++; map[nowx][nowy] = map[nextx][nexty]; nowx = nextx, nowy = nexty; } while (! (nextx ==i && nexty == i ) ); nowx -= movel[dir][moving][0], nowy -= movel[dir][moving][1]; map[nowx][nowy] = temp; } } int main() { string line; while (getline(cin, line) ) { // read data string order = line; getline(cin, line); size = (order[0]-48)*10 +order[1]-48; if (size==0) size=100; // build map int i=1, j=1; F(k,0, line.length()-1 ) { map[i][j++] = toupper( line[k] ); if (j>size) j = 1, i++; } if (i<=size) { int now = 65; while (i<=size) { map[i][j++] = now++; if (j>size) j=1, i++; if (now>90) now = 65; } } // solve F(i,2,order.length()-1) { switch (order[i]) { case 'R': rattle(); break; case 'S': shake(); break; case 'L': loop(); } } // output F(i,1,size) F(j,1,size) printf("%c", map[i][j]); printf("/n"); } return 0; }