跳到正文

题解

题解:P17100 [ICPC 2017 Qingdao R] The Squared Mosquito Coil

nyt_nyt2012约 3 分钟
原文
文章信息与版本

文章信息

发布
2026年7月12日
收录
2026年8月31日
更新
2026年11月11日
篇幅
约 3 分钟
获赞
1
题目
P17100

版本

  1. v1当前

简单模拟,依题意模拟即可。

考虑用一个 0∼30\sim3 之间整数 dd 表示方向,每次转方向(只有右转 90∘90^{\circ})就是 (d+1)%4,也是 (d+1)&3。这样我们直接暴力跳,每到碰边界或原蚊香是调转方向,直到无法走为止,用几个 while 循环即可实现。在存两个扩展数组 d1,d2d1,d2 就可以实现根据方向跳了。

AC Code
C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
int d1[4]={0,1,0,-1},d2[4]={1,0,-1,0},t,n,g[45][45],r,c,d;
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    cin>>t;
    while(t--){
        cin>>n;
        memset(g,0,sizeof(g));
        r=c=d=0;g[0][0]=1;
        while(1){
            int x=0;
            while(1){
                int n1=r+d1[d],n2=c+d2[d];
                if(n1<0||n1>=n||n2<0||n2>=n)break;
                if(g[n1][n2])break;
                int cnt=0;
                for(int i=0;i<4;i++){
                    int t1=n1+d1[i],t2=n2+d2[i];
                    if(t1>=0&&t1<n&&t2>=0&&t2<n&&g[t1][t2])cnt++;
                }
                if(cnt==1){
                    r=n1;c=n2;
                    g[r][c]=1;
                    x++;
                }
                else break;
            }
            if(x==0)break;
            d++;d&=3;
        }
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++)cout<<(g[i][j]?'#':' ');
            cout<<'\n';
        }
    }
    return 0;
}
原文