题解:P17100 [ICPC 2017 Qingdao R] The Squared Mosquito Coil
nyt_nyt2012 · · 题解
简单模拟,依题意模拟即可。
考虑用一个 (d+1)%4,也是 (d+1)&3。这样我们直接暴力跳,每到碰边界或原蚊香是调转方向,直到无法走为止,用几个 while 循环即可实现。在存两个扩展数组
:::success[AC Code]
#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;
}
:::