Secret Poems - HihoCoder - 1632

  图一                图二

Following the order indicated by arrows, you can get “THISISAVERYGOODPOEMITHINK”, and that can mean something.But after some time, poets found out that some Yongzheng’s secret agent called “Mr. blood dripping” could read this kind of poems too. That was dangerous. So they introduced a new order of writing poems as shown in figure 2. And they wanted to convert the old poems written in old order as figure1 into the ones in new order. Please help them.InputThere are no more than 10 test cases.For each test case:The first line is an integer N( 1 <= N <= 100), indicating that a poem is a N×N matrix which consist of capital letters.Then N lines follow, each line is an N letters string. These N lines represent a poem in old order.OutputFor each test case, convert the poem in old order into a poem in new order.

Sample Input

5
THSAD
IIVOP
SEOOH
RGETI
YMINK
2
AB
CD
4
ABCD
EFGH
IJKL
MNOP

Sample Output

THISI
POEMS
DNKIA
OIHTV
OGYRE
AB
DC
ABEI
KHLF
NPOC
MJGD

大致题意:

  就是先按照图一的方式进行遍历这个图——按照图一的顺序存成一个字符串;然后按照图二的“回字形 ”顺序进行调整,然后输出图二!

AC题解:

  没错,题意就是这么简单!写的时候一不留神就出了很多BUG!然后改来改去花费了不少的时间!还是得细心点点!

  没有想到简单的办法,只好进行If...else...的嵌套!

 #include<stdio.h>
#include<math.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<set>
#include<vector>
#define inf 0x3f3f3f3f //Pangu and Stones ,I
#define ll long long
using namespace std;
#define N 108
char a[N][N];
char ans[N][N];
bool vis[N][N];
int dir[][]={ {,},{,},{,-},{-,},{,-},{-,} };
void factans(string s,int n){
int x=,y=;
memset(vis,false,sizeof(vis));
vis[][]=true;
memset(ans,'',sizeof(ans));
ans[][]=s[];
int op=-;
//先按之前的进行,右下左上右————
for(int i=;i<s.length();i++){
if(op!=-){
int dx=x+dir[op][];
int dy=y+dir[op][];
if(dx>=&&dx<=n-&&dy>=&&dy<=n-&&vis[dx][dy]==false){
x=dx;y=dy;
ans[x][y]=s[i];
vis[x][y]=true;
continue;
}
}
if(y+<=n-&&vis[x][y+]==false){//右下左上右————
ans[x][y+]=s[i];
y++;
op=;vis[x][y]=true;
}else if(x+<=n-&&vis[x+][y]==false){
ans[x+][y]=s[i];
x++;
op=;vis[x][y]=true;
}else if(y->=&&vis[x][y-]==false){
ans[x][y-]=s[i];
y--;
op=;vis[x][y]=true;
}else if(x->=&&vis[x-][y]==false){
ans[x-][y]=s[i];
x--;
op=;vis[x][y]=true;
}
}
for(int i=;i<n;i++)
ans[i][n]='\0';
for(int i=;i<n;i++)
printf("%s\n",ans[i]); }
int main(){
int n; while(scanf("%d",&n)!=EOF){
//int num=1;
for(int i=;i<n;i++)
scanf("%s",a[i]);
string s;
s.clear();
int i=,j=;
int op=-;
memset(vis,false,sizeof(vis));
while(){ s+=a[i][j];//存储当前位置 if(i==n-&&j==n-){
// cout<<"end: "<<s<<endl;
break;
} vis[i][j]=true; if(i==){//在第一行,可右0,可左下4,可下1
if(j+<=n-&&op!=){
i+=dir[][];
j+=dir[][];
op=;
}
else if(i+<=n-&&j->=&&vis[i+][j-]==false){
i+=dir[][];
j+=dir[][];
op=;
}
else{
i+=dir[][];
j+=dir[][];
op=;
}
}
else if(j==){//在第一列,先下1,后右上5,后右0
if(op!=&&i+<=n-){
i+=dir[][];
j+=dir[][];
op=;
}else if(i->=&&j+<=n-&&vis[i-][j+]==false){
i+=dir[][];
j+=dir[][];
op=;
}
else{
i+=dir[][];
j+=dir[][];
op=;
}
}
else if(i==n-){//最后一行,先右上5,后右0
if(i->=&&j+<=n-&&vis[i-][j+]==false){
i+=dir[][];
j+=dir[][];
op=;
}
else{
i+=dir[][];
j+=dir[][];
op=;
}
}
else if(j==n-){//最后一列,先左下4,后下1
if(i+<=n-&&j->=&&vis[i+][j-]==false){
i+=dir[][];
j+=dir[][];
op=;
}
else if(op!=){
i+=dir[][];
j+=dir[][];
op=;
}
}
else{//其余的照旧就可以了
i+=dir[op][];
j+=dir[op][];
}
}
factans(s,n); }
return ;
}

(有详细的解释呦!)

最新文章

  1. [转]extjs render 用法介绍
  2. MyEclipse 10.7.1 最新版官网下载地址
  3. Foundation -----&gt;NSDictionary
  4. WIN-CE系统架构
  5. Xcode push带有cocoapods类库的项目到git仓库
  6. [转]Informatica vs SSIS
  7. 34. Convert Sorted List to Binary Search Tree &amp;&amp; Convert Sorted Array to Binary Search Tree
  8. 跨平台网络库(采用C++ 11)
  9. poj 1664 put apples(dfs)
  10. Spring MVC之视图解析器
  11. MongoDB数据库安装及配置环境终极教程(windows10系统)
  12. ExecutorService的submit方法使用
  13. MyBatis3系列__02接口式编程
  14. (28)django的中间件(自定义中间件和防范跨站请求伪造攻击)-重要的概念
  15. 51Nod 1815 调查任务
  16. [No000018E]Vim快速跳转任意行、任意列以及高亮显示当前行、当前列方法-Vim使用技巧(3)
  17. 循环取到json中的字段数据,加到html中
  18. layout_weight 全解析
  19. 解决css3不支持同时缩放和旋转的办法
  20. Delphi中Unicode转中文

热门文章

  1. virtualbox 安装扩展包 VirtualBox Extension Pack
  2. 基于vue-cli、elementUI的Vue简单入门例子
  3. Redis从认识安装到实现增删改查
  4. LeetCode 206. 反转链表(Reverse Linked List) 16
  5. IDEA 的操作与使用
  6. 学习makefile 的网址
  7. 人机交互技术 Week 11_Data gathering
  8. python 之 并发编程(线程Event、协程)
  9. 面试题之web访问突然延迟问题
  10. &#39;telent&#39; 不是内部或外部命令,也不是可运行的程序或批处理文件。