跳到正文

题解

题解:P16183 [ICPC 2014 NAIPC] Two Knight' s Poem

zhoubingyan2012约 6 分钟
原文
文章信息与版本

文章信息

发布
2026年6月6日
收录
2026年8月25日
更新
2026年11月14日
篇幅
约 6 分钟
题目
P16183

版本

  1. v1当前

思路

把 4040 个按键转成一维,使用 bfs 进行匹配。 设当前状态 i,p1,p2i, p1, p2 表示匹配到第 ii 个字符,骑士1在位置 p1p1,骑士2在位置 p2p2。

考虑状态转移。 枚举 p1p1 的所有合法目标 npnp,骑士不能占据同一个按键,所以要求 np≠p2np \neq p2。
若 np1np1 是 Shift 键,则不输出字符,加入状态 i,np,p2i, np, p2。
否则,根据 p2p2 是否为 Shift 决定输出字符,如果与诗歌的字符匹配,则加入状态 i+1,np,p2i + 1, np, p2。

骑士2的转移同理,枚举 p2p2 的合法目标 npnp 对称处理即可。

注意题目输入为 CRLF 格式,每一行的末尾是 \r\n,需要手动把最后的 \r 去掉。

状态总数为约为 n×402=1.6×105n \times 40^2 = 1.6 \times 10^5,可以轻松通过。

Code

C++
#include<bits/stdc++.h>
using namespace std;
const int N=40,R=4,C=10;
//键盘布局
char l[R][C+5]={
    "qwertyuiop",
    "asdfghjkl;",
    "zxcvbnm,./"
},u[R][C+5]={
    "QWERTYUIOP",
    "ASDFGHJKL:",
    "ZXCVBNM<>?"
};
int dx[]={-2,-2,-1,-1,1,1,2,2};
int dy[]={1,-1,2,-2,2,-2,1,-1};
//将坐标转换为一维编号
int id(int x,int y){ return x*10+y; }
bool shift(int x,int y){
    if(x!=3) return false;
    return y<=1||y>=8;
}
string s;
vector<int> g[N];
char ch[N][2];
bool vis[105][N][N];
void init(){
    for(int i=0;i<R;i++)
        for(int j=0;j<C;j++){
            int idx=id(i,j);
            //设置该按键的输出字符
            if(i==3&&!shift(i,j)) ch[idx][0]=' ',ch[idx][1]=' ';//空格键
            else ch[idx][0]=l[i][j],ch[idx][1]=u[i][j];
            //构建邻接表
            for(int k=0;k<8;k++){
                int nx=i+dx[k],ny=j+dy[k];
                if(nx<0||nx>=R||ny<0||ny>=C) continue;
                g[idx].push_back(id(nx,ny));
            }
        }
}
bool solve(){
    int n=s.size();
    memset(vis,false,sizeof(vis));
    struct rec{ int x,p1,p2; };
    queue<rec> q;
    int s1=id(3,0),s2=id(3,9);//初始位置:左Shift和右Shift
    q.push({0,s1,s2});
    vis[0][s1][s2]=true;
    while(!q.empty()){
        auto [i,p1,p2]=q.front(); q.pop();
        if(i==n) return true;//已匹配完所有字符
        //移动骑士1
        for(int np:g[p1]){
            if(np==p2) continue;//不能占据同一个按键
            char c=ch[np][shift(p2/C,p2%C)];
            if(c==0){
                //落在Shift键上,不输出字符
                if(vis[i][np][p2]) continue;
                vis[i][np][p2]=true;
                q.push({i,np,p2});
            }else if(c==s[i]){
                //输出字符匹配,推进进度
                if(vis[i+1][np][p2]) continue;
                vis[i+1][np][p2]=true;
                q.push({i+1,np,p2});
            }
        }
        //对称处理骑士2
        for(int np:g[p2]){
            if(np==p1) continue;
            char c=ch[np][shift(p1/C,p1%C)];
            if(c==0){
                if(vis[i][p1][np]) continue;
                vis[i][p1][np]=true;
                q.push({i,p1,np});
            }else if(c==s[i]){
                if(vis[i+1][p1][np]) continue;
                vis[i+1][p1][np]=true;
                q.push({i+1,p1,np});
            }
        }
    }
    return false;
}
int main(){
    init();
    while(getline(cin,s)){
        if(s=="*") break;
        s.pop_back();//去除'\r'
        cout<<solve()<<endl;
    }
    return 0;
}
原文