Secret Poems 2017ACM-ICPC北京赛区/hihoCoder1632

题意: 题目意思就是将两个不同规则的字符数组进行相互转换。

思路: 将题目的解答分成两部分,第一部分是将 figure 1 的数组转换成一维数组,第二部分是将一维数组中的字符填入 figure 2 的数组中。

针对第一部分,将 figure 1 的二维数组旋转四十五度再观察,能够发现奇数行是从左往右,偶数行是从右往左;上半部分中,每行边上的结点移动是右、下不断更替,下半部分中,每行边上的结点移动是下、右不断更替。

对于第二部分,通过对当前位置的判断进行移动,直到填满为止。

代码:

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<stack>
#include<queue>
#include<utility>
#include<vector>
#include<cmath>
#include<set>
#include<map>
#include<iostream>
#include<algorithm>
#include<sstream>
using namespace std;
typedef long long LL;

int N;
char G[110][110];
char ch[10010];
char ans[110][110];



int main()
{
    //freopen("in.txt", "r", stdin);
    while(scanf("%d", &N) == 1){
        for(int i=1; i<=N; i++){
            scanf("%s", &G[i][1]);
        }

        int x=1, y=1;
        int tot = 0;
        bool right = true;
        bool bhalf = true;
        for(int i=1; i<=2*N-1; i++){
            if(i == N) bhalf = false;
            ch[tot++] = G[x][y];
            for(int j=1; j<min(i, 2*N-i); j++){
                if(i & 1){
                    x--; y++;
                }
                else{
                    x++; y--;
                }
                ch[tot++] = G[x][y];
            }

            if(right){
                if(bhalf) y++;
                else x++;
            }
            else{
                if(bhalf) x++;
                else y++;
            }
            right = !right;
        }

        ch[tot] = '\0';
        //printf("%s\n", ch);

        memset(ans, 0, sizeof(ans));
        x = 1, y = 1;
        tot = 0;
        int mov = 0;  //0 表示向右,1 表示向下,2 表示向左,3 表示向上
        while(true){

            ans[x][y] = ch[tot++];
            if(tot == N*N) break;

            if(x==1&&y==N || isupper(ans[x-1][y])&&!isupper(ans[x+1][y])&&isupper(ans[x][y-1])&&isupper(ans[x][y+1])) mov = 1;
            else if(x==N&&y==N || isupper(ans[x-1][y])&&isupper(ans[x+1][y])&&!isupper(ans[x][y-1])&&isupper(ans[x][y+1])) mov = 2;
            else if(x==N&&y==1 || !isupper(ans[x-1][y])&&isupper(ans[x+1][y])&&isupper(ans[x][y-1])&&isupper(ans[x][y+1])) mov = 3;
            else if(y==1&&isupper(ans[x-1][y]) || isupper(ans[x-1][y])&&isupper(ans[x+1][y])&&isupper(ans[x][y-1])&&!isupper(ans[x][y+1])) mov = 0;

            if(mov == 0) y++;
            else if(mov == 1) x++;
            else if(mov == 2) y--;
            else if(mov == 3) x--;
        }

        for(int i=1; i<=N; i++){
            for(int j=1; j<=N; j++){
                putchar(ans[i][j]);
            }
            putchar('\n');
        }
    }
    return 0;
}
要下载 `poems.txt` 数据集,通常可以通过以下几种方法实现: ### 方法一:手动查找并下载 如果该数据集已经公开发布,则可以访问其官方存储位置或托管平台(如 GitHub、Kaggle 或其他学术资源站点)。以下是可能的操作流程: 1. 访问已知的在线数据库或项目页面。 2. 查找是否有提供直接下载链接。 3. 使用浏览器点击下载按钮或将命令复制到终端执行。 例如,在 Linux/MacOS 终端或者 Windows PowerShell 中运行 wget 命令来获取远程文件: ```bash wget https://example.com/path/to/poems.txt ``` 注意替换上面 URL 地址为实际有效的网络地址[^1]。 ### 方法二:编写脚本自动抓取 当无法找到现成的数据包时,也可以考虑利用 Python 编程语言配合 requests 库以及 BeautifulSoup 等工具自行采集相关内容。下面给出一段简单的示范代码用于模拟从某假定网站提取古诗词信息并保存至本地文本文件的过程: ```python import requests from bs4 import BeautifulSoup url = "http://some-poetry-site.example/" # 替换为目标网址 response = requests.get(url) soup = BeautifulSoup(response.content, 'html.parser') titles = soup.find_all('h2') # 调整选择器匹配具体结构 verses = [] for t in titles: verse_text = "" next_node = t.next_sibling while True: if not next_node or isinstance(next_node, str) and '\n' != next_node.strip(): break elif '<br/>' == repr(next_node).lower().strip(): pass else: verse_line = ''.join([str(x) for x in list(next_node.children)]) verse_text += verse_line + "\n" next_node = next_node.nextSibling verses.append((t.text.strip(),verse_text)) with open("poems.txt","w+",encoding="utf-8") as f_out: for v in verses: f_out.write("{}:{}\n".format(*v)) ``` 此段程序会遍历指定网页上的所有 h2 元素作为标题,并尝试捕获紧随其后的诗句内容直到遇到下一个同级节点为止。最终结果将以冒号分隔的形式记录下来以便后续分析处理[^4]。 另外需要注意的是,某些情况下目标服务器可能会设置反爬机制阻止频繁请求行为;此时建议适当加入延时逻辑减少干扰风险的同时尊重对方服务条款规定。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值