2017年ICPC中国大陆区域赛真题(上)B题

题目网址点击进入

problem
The Yongzheng Emperor (13 December 1678 – 8 October 1735), was the fifth emperor of the Qing dynasty of China. He was a very hard-working ruler. He cracked down on corruption and his reign was known for being despotic, efficient, and vigorous.

Yongzheng couldn’t tolerate people saying bad words about Qing or him. So he started a movement called “words prison”. “Words prison” means literary inquisition. In the famous Zhuang Tinglong Case, more than 70 people were executed in three years because of the publication of an unauthorized history of the Ming dynasty.

In short, people under Yongzheng’s reign should be very careful if they wanted to write something. So some poets wrote poems in a very odd way that only people in their friends circle could read. This kind of poems were called secret poems.

A secret poem is a N×N matrix of characters which looks like random and meaning nothing. But if you read the characters in a certain order, you will understand it. The order is shown in figure 1 below:
在这里插入图片描述
Input
There 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.

Output
For 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

大致题意
第一行输入一个数字n,之后是n*n的一个矩阵字符,按照图片规律排版完后输出,左边按照边角S走位,右边这是回型走位

思路
这题主要的关键在于作图对角S路线的寻找,也许有人是用数学规律寻找出来的,但博主比较笨,用了一个暴力的方法,我们可以明显的发现当斜着向上走的时候,没有路,就会走向右,当右边也没有路的时候就会向下,而当向左下走的时候,没有路就会向下,如果下也没有就会向右,这就是一个规律,当走到末端n,n的时候停止
右边的图规律相对简单,右下左上循环,碰到边界就转弯

代码

#include<stdio.h>
#include<string.h>
using namespace std;
int main()
{
    int pome[105][105],pome2[105][105];///左边的图和右边的图
    int n;
    while (scanf("%d%*c",&n)!=EOF)
    {
        memset(pome,0,sizeof(pome));///全部归0设置边界
        memset(pome2,0,sizeof(pome2));
        for (int i=1;i<=n;i++)
        {
            char a[105];
            scanf("%s%*c",&a);
            for (int j=1;j<=n;j++)
            {
                pome[i][j]=a[j-1];///将字符串转化数字储存
            }
        }
        int operation=1,x=1,y=1,changex=1,changey=1,operation2=0;
        /// 左图向右上左下走,x,y左边图坐标,cx,cy右边图坐标,右图方向
        while (x!=n||y!=n)///当到达末尾时候停止
        {
            pome2[changex][changey]=pome[x][y];
            if (operation==1)///1时向右上
            {
                if (pome[x-1][y+1]==0)///走不了
                {
                    if (pome[x][y+1]==0)///是否可以向右
                    {
                        x++;///向右走
                    }else
                    {
                        y++;///向下走
                    }
                    operation=2;///转弯后走的方向改变
                }else
                {
                    x--;///右上走
                    y++;
                }
            }else
            {///2时向左下走
                if (pome[x+1][y-1]==0)///走不了
                {
                    if (pome[x+1][y]==0)///是否可以向下
                    {
                        y++;///向下走
                    }else
                    {
                        x++;///向右走
                    }
                    operation=1;///转弯后走的方向改变
                }else
                {
                    x++;///左下走
                    y--;
                }
            }
            if (operation2%4==0)///0向右
            {
                changey++;
                if (changey==n||pome2[changex][changey+1]!=0)///遇见边界
                    operation2++;
            }else if (operation2%4==1)///1向下
            {
                changex++;
                if (changex==n||pome2[changex+1][changey]!=0)///遇见边界
                    operation2++;
            }else if (operation2%4==2)///2向左走
            {
                changey--;
                if (changey==1||pome2[changex][changey-1]!=0)///遇见边界
                    operation2++;
            }else if (operation2%4==3)///3向上走
            {
                changex--;
                if (changex==1||pome2[changex-1][changey]!=0)///遇见边界
                    operation2++;
            }
        }
        pome2[changex][changey]=pome[x][y];///循环到终点停止没有赋值,这里补上
        for (int i=1;i<=n;i++)
        {
            for (int j=1;j<=n;j++)
                printf("%c",pome2[i][j]);
            printf("\n");
        }
    }

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值