考场上的:
#include<bits/stdc++.h>
using namespace std;
#define int long long
char a[2001][2001];
int dx[5] = {0,1,0,-1};
int dy[5] = {1,0,-1,0};
bool f[10000001];
signed main()
{
int T;
cin>>T;
while(T --)
{
memset(f,0,sizeof(f));
int n,m,k;
cin>>n>>m>>k;
int x,y,d;
cin>>x>>y>>d;
f[1000 * x + y] = true;
for(int i = 1;i <= n;i ++)
{
for(int j = 1;j <= m;j ++)
{
cin>>a[i][j];
}
}
for(int i = 1;i <= k;i ++)
{
int nx = x + dx[d];
int ny = y + dy[d];
if(a[nx][ny] == 'x' || nx < 1 || nx > n || ny < 1 || ny > m)
{
d = (d + 1) % 4;
}
else
{
x = nx,y = ny;
f[1000 * x + y] = true;
}
}
int ans = 0;
for(int i = 0;i <= 1000001;i ++)ans += f[i];
cout<<ans<<endl;
}
return 0;
}
后来改的(100pts):
#include<bits/stdc++.h>
using namespace std;
#define int long long
char a[2001][2001];
int dx[5] = {0,1,0,-1};
int dy[5] = {1,0,-1,0};
//bool f[10000001];
map <pair<int,int>,bool> mp;
signed main()
{
int T;
cin>>T;
while(T --)
{
map <pair<int,int>,bool> mp;
// memset(f,0,sizeof(f));
int n,m,k;
cin>>n>>m>>k;
int x,y,d;
cin>>x>>y>>d;
// f[1000 * x + y] = true;
mp[{x,y}] = true;
for(int i = 1;i <= n;i ++)
{
for(int j = 1;j <= m;j ++)
{
cin>>a[i][j];
}
}
for(int i = 1;i <= k;i ++)
{
int nx = x + dx[d];
int ny = y + dy[d];
if(a[nx][ny] == 'x' || nx < 1 || nx > n || ny < 1 || ny > m)
{
d = (d + 1) % 4;
}
else
{
x = nx,y = ny;
mp[{x,y}] = true;
}
}
// int ans = 0;
// for(int i = 0;i <= 1000001;i ++)ans += f[i];
// cout<<ans<<endl;
cout<<mp.size()<<endl;
}
return 0;
}
这里主要帮忙看一看如果就不用map,就用我这种方法能不能改出来,这里给大家专门讲一下,有人可能认为是1000*x+y那里有问题,因为map就能对,这个不能对就非常诡异,但是由这个代码:
#include<bits/stdc++.h>
using namespace std;
int f[10000001];
int main()
{
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
for(int i = 1;i <= 1000;i ++)
{
for(int j = 1;j <= 1000;j ++)
{
f[i * 1000 + j] ++;
}
}
for(int i = 1;i <= 1000;i ++)
{
for(int j = 1;j <= 1000;j ++)
{
cout<<f[i * 1000 + j]<<" ";
if(f[i * 1000 + j] >= 2)while(1);
}
cout<<endl;
}
return 0;
}
又可以证明所有1000*x + y的值是不重复的,这就很诡异,望巨佬条一条