LamineYamal @ 2024-10-26 18:08:12
#include<bits/stdc++.h>
#define endl "\n"
#define fast ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
using namespace std;
const int N=1e3+5;
int T,n,m,k,x,y,d,cnt,arr[N][N],maps[N][N];
void walk(int nx,int ny,int nd)
{
if(nd==0)
{
ny+=1;
}
if(nd==1)
{
nx+=1;
}
if(nd==2)
{
ny-=1;
}
if(nd==3)
{
nx-=1;
}
if(nx>n||nx<1||ny>m||ny<1||arr[nx][ny]==2)
{
d=(nd+1)%4;
return ;
}
else
{
if(maps[nx][ny]==0)
{
cnt++;
maps[nx][ny]=1;
}
x=nx;
y=ny;
}
return ;
}
int main()
{
fast;
cin>>T;
while(T--)
{
memset(maps,0,sizeof(maps));
cnt=1;
cin>>n>>m>>k>>x>>y>>d;
for(int i=1;i<=n;i++)
{
string a;
cin>>a;
for(int j=0;j<m;j++)
{
if(a[j]=='.')
{
arr[i][j+1]=1;
}
else
{
arr[i][j+1]=2;
}
}
}
for(int i=1;i<=k;i++)
{
walk(x,y,d);
}
cout<<cnt<<endl;
}
return 0;
}
by Cloke0 @ 2024-10-26 18:51:23
暴力,O(T*k),比赛时样例5死活不出答案,以为超时,结果AC
#include<bits/stdc++.h>
using namespace std;
int T,n,m,k,x,y,d;
int main(){
scanf("%d",&T);
while(T--){
scanf("%d%d%d",&n,&m,&k);
getchar();
scanf("%d%d%d",&x,&y,&d);
char c[n+1][m+1];
int v[n+1][m+1]={},ans=0;
for(int i=1;i<=n;i++){
scanf("%s",&c[i]);
}
v[x][--y]=1;
while(k--){
int nx,ny;
if(d==0){nx=x,ny=y+1;}
if(d==1){nx=x+1,ny=y;}
if(d==2){nx=x,ny=y-1;}
if(d==3){nx=x-1,ny=y;}
if(c[nx][ny]=='x'||nx<1||nx>n||ny<0||ny>=m){
d=(d+1)%4;
}
else{
x=nx,y=ny;
v[x][y]=1;
}
}
for(int i=1;i<=n;i++){
for(int j=0;j<m;j++){
if(v[i][j])ans++;
}
}
printf("%d\n",ans);
}
return 0;
}
by Xuancheng_Mao @ 2024-10-26 19:00:43
@Cloke0 什么暴力,这题不就是一个模拟吗(
by LamineYamal @ 2024-10-26 19:32:12
@Xuancheng_Mao 我把赛时代码记错了,少了个标记