zhouzhaoyan1 @ 2024-11-01 15:36:50
#include <bits/stdc++.h>
using namespace std;
char a[1010][1010];
int main()
{
int t;
cin>>t;
while(t--)
{
int n,m,k,x,y,d,z=0;
cin>>n>>m>>k;
cin>>x>>y>>d;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
cin>>a[i][j];
}
}
while(k--)
{
if(d==0)
{
if(x<=n&&y+1<=m&&x>=1&&y+1>=1&&a[x][y+1]!='x')
{
a[x][y]='1';
y+=1;
a[x][y]='1';
}
else
{
d+=1;
d%=4;
}
}
else if(d==1)
{
if(x+1<=n&&y<=m&&x+1>=1&&y>=1&&a[x+1][y]!='x')
{
a[x][y]='1';
x+=1;
a[x][y]='1';
}
else
{
d+=1;
d%=4;
}
}
else if(d==2)
{
if(x<=n&&y-1<=m&&x>=1&&y-1>=1&&a[x][y-1]!='x')
{
a[x][y]='1';
y-=1;
a[x][y]='1';
}
else
{
d+=1;
d%=4;
}
}
else if(d==3)
{
if(x-1<=n&&y<=m&&x-1>=1&&y>=1&&a[x-1][y]!='x')
{
a[x][y]='1';
x-=1;
a[x][y]='1';
}
else
{
d+=1;
d%=4;
}
}
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(a[i][j]=='1')
{
z++;
}
}
}
cout<<z<<endl;
}
return 0;
}
by Zxm2013 @ 2024-11-03 18:14:56
@zhouzhaoyan1
我的代码:
#include<iostream>
using namespace std;
int t;
int main(){
cin>>t;
for(int i=0;i<t;i++){
int n,m,k,x,y,r,ans=0,a[1010][1010]={0},b[1010][1010]={0};
cin>>n>>m>>k>>x>>y>>r;
for(int j=1;j<=n;j++){
for(int q=1;q<=m;q++){
char num;
cin>>num;
if(num=='.'){
a[j][q]=1;
}
}
}
b[x][y]=1;
for(int j=1;j<=k;j++){
if(r==0){
if(a[x][y+1]==1){
if(b[x][y+1]==0){
ans++;
b[x][y+1]=1;
}
y++;
}
else r=(r+1)%4;
}
else if(r==1){
if(a[x+1][y]==1){
if(b[x+1][y]==0){
ans++;
b[x+1][y]=1;
}
x++;
}
else r=(r+1)%4;
}
else if(r==2){
if(a[x][y-1]==1){
if(b[x][y-1]==0){
ans++;
b[x][y-1]=1;
}
y--;
}
else r=(r+1)%4;
}
else {
if(a[x-1][y]==1){
if(b[x-1][y]==0){
ans++;
b[x-1][y]=1;
}
x--;
}
else r=(r+1)%4;
}
}
cout<<ans+1<<endl;
}
return 0;
}
求关,我们可以互相学习~
by zhouzhaoyan1 @ 2024-11-04 21:37:48
谢谢