zoukankan      html  css  js  c++  java
  • 【*的数组模拟】Secret Poems

    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...的嵌套!

      1 #include<stdio.h>
      2 #include<math.h>
      3 #include<string.h>
      4 #include<iostream>
      5 #include<algorithm>
      6 #include<string>
      7 #include<set>
      8 #include<vector>
      9 #define inf 0x3f3f3f3f   //Pangu and Stones ,I
     10 #define ll long long
     11 using namespace std;
     12 #define N 108
     13 char a[N][N];
     14 char ans[N][N];
     15 bool vis[N][N];
     16 int dir[6][2]={ {0,1},{1,0},{0,-1},{-1,0},{1,-1},{-1,1} };
     17 void factans(string s,int n){
     18     int x=0,y=0;
     19     memset(vis,false,sizeof(vis));
     20     vis[0][0]=true;
     21     memset(ans,'6',sizeof(ans));
     22     ans[0][0]=s[0];
     23     int op=-1;
     24             //先按之前的进行,右下左上右————
     25    for(int i=1;i<s.length();i++){
     26         if(op!=-1){
     27                 int dx=x+dir[op][0];
     28                 int dy=y+dir[op][1];
     29             if(dx>=0&&dx<=n-1&&dy>=0&&dy<=n-1&&vis[dx][dy]==false){
     30                x=dx;y=dy;
     31                 ans[x][y]=s[i];
     32                 vis[x][y]=true;
     33                 continue;
     34             }
     35         }
     36         if(y+1<=n-1&&vis[x][y+1]==false){//右下左上右————
     37             ans[x][y+1]=s[i];
     38             y++;
     39             op=0;vis[x][y]=true;
     40         }else if(x+1<=n-1&&vis[x+1][y]==false){
     41             ans[x+1][y]=s[i];
     42             x++;
     43             op=1;vis[x][y]=true;
     44         }else if(y-1>=0&&vis[x][y-1]==false){
     45             ans[x][y-1]=s[i];
     46             y--;
     47             op=2;vis[x][y]=true;
     48         }else if(x-1>=0&&vis[x-1][y]==false){
     49             ans[x-1][y]=s[i];
     50             x--;
     51             op=3;vis[x][y]=true;
     52         }
     53    }
     54    for(int i=0;i<n;i++)
     55         ans[i][n]='';
     56    for(int i=0;i<n;i++)
     57         printf("%s
    ",ans[i]);
     58 
     59 }
     60 int main(){
     61     int n;
     62 
     63     while(scanf("%d",&n)!=EOF){
     64     //int num=1;
     65         for(int i=0;i<n;i++)
     66             scanf("%s",a[i]);
     67          string s;
     68          s.clear();
     69          int i=0,j=0;
     70          int op=-1;
     71          memset(vis,false,sizeof(vis));
     72          while(1){
     73 
     74             s+=a[i][j];//存储当前位置
     75 
     76             if(i==n-1&&j==n-1){
     77               //  cout<<"end: "<<s<<endl;
     78                     break;
     79             }
     80 
     81             vis[i][j]=true;
     82 
     83             if(i==0){//在第一行,可右0,可左下4,可下1
     84                 if(j+1<=n-1&&op!=0){
     85                     i+=dir[0][0];
     86                     j+=dir[0][1];
     87                     op=0;
     88                 }
     89                 else if(i+1<=n-1&&j-1>=0&&vis[i+1][j-1]==false){
     90                     i+=dir[4][0];
     91                     j+=dir[4][1];
     92                     op=4;
     93                 }
     94                 else{
     95                     i+=dir[1][0];
     96                     j+=dir[1][1];
     97                     op=1;
     98                 }
     99             }
    100             else if(j==0){//在第一列,先下1,后右上5,后右0
    101                 if(op!=1&&i+1<=n-1){
    102                     i+=dir[1][0];
    103                     j+=dir[1][1];
    104                     op=1;
    105                 }else if(i-1>=0&&j+1<=n-1&&vis[i-1][j+1]==false){
    106                      i+=dir[5][0];
    107                     j+=dir[5][1];
    108                     op=5;
    109                 }
    110                 else{
    111                      i+=dir[0][0];
    112                     j+=dir[0][1];
    113                     op=0;
    114                 }
    115             }
    116             else if(i==n-1){//最后一行,先右上5,后右0
    117                 if(i-1>=0&&j+1<=n-1&&vis[i-1][j+1]==false){
    118                      i+=dir[5][0];
    119                     j+=dir[5][1];
    120                     op=5;
    121                 }
    122                 else{
    123                      i+=dir[0][0];
    124                     j+=dir[0][1];
    125                     op=0;
    126                 }
    127             }
    128             else if(j==n-1){//最后一列,先左下4,后下1
    129                 if(i+1<=n-1&&j-1>=0&&vis[i+1][j-1]==false){
    130                     i+=dir[4][0];
    131                     j+=dir[4][1];
    132                     op=4;
    133                 }
    134                 else if(op!=1){
    135                      i+=dir[1][0];
    136                     j+=dir[1][1];
    137                     op=1;
    138                 }
    139             }
    140             else{//其余的照旧就可以了
    141                 i+=dir[op][0];
    142                 j+=dir[op][1];
    143             }
    144         }
    145         factans(s,n);
    146 
    147     }
    148     return 0;
    149 }
    View Code(有详细的解释呦!)
  • 相关阅读:
    转帖:解决从9.2.0.1升级到9.2.0.7出现的错误
    最近在公司内部作了一次WCF的培训
    SourceSafe的命令行
    公司再过一两个月就要关门了
    MimeType
    ORACLE 10G 如何使用超过1.7G的内存
    切换网卡
    热键
    Oracle数据库碎片整理
    Hydra安装与使用
  • 原文地址:https://www.cnblogs.com/zhazhaacmer/p/8640961.html
Copyright © 2011-2022 走看看