programmer330 @ 2024-10-26 23:00:51
#include<bits/stdc++.h>
using namespace std;
int t;
int n,m,k,d,xx,yy,dd;
int x,y;
char a[1001][1001];
bool check[1001][1001];
bool again[1001][1001];
int main(){
cin>>t;
for(int T=1;T<=t;T++){
cin>>n>>m>>k>>xx>>yy>>dd;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
cin>>a[i][j];
check[i][j]=false;
again[i][j]=false;
}
}
d=dd;
x=xx;
y=yy;
check[x][y]=true;
int sum=0;
for(int i=1;i<=k;i++){
if(d==0){
if (a[x][y+1]=='.'&&y+1<=m){
y+=1;
if(again[x][y]==true)sum-=1;
check[x][y]=true;
again[x][y]=true;
}else{
d=(d+1)%4;
}
}
else if(d==1){
if (a[x+1][y]=='.'&&x+1<=n){
x+=1;
if(again[x][y]==true)sum-=1;
check[x][y]=true;
again[x][y]=true;
}else{
d=(d+1)%4;
}
}
else if(d==2){
if (a[x][y-1]=='.'&&y-1>0){
y-=1;
if(again[x][y]==true)sum-=1;
check[x][y]=true;
again[x][y]=true;
}else{
d=(d+1)%4;
}
}
else if(d==3){
if (a[x-1][y]=='.'&&x-1>0){
x-=1;
if(again[x][y]==true)sum-=1;
check[x][y]=true;
again[x][y]=true;
}else{
d=(d+1)%4;
}
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(check[i][j]==true)sum+=1;
}
}
cout<<sum<<endl;
}
return 0;
}
by Ahws_rwhy @ 2024-10-26 23:03:39
@programmer330 我的思路跟你挺像的,可以参考一下
by Ahws_rwhy @ 2024-10-26 23:04:47
#include <bits/stdc++.h>
using namespace std;
int t;
int n, m, k;
int x, y, d;
char mp[1001][1001];
int flag[1001][1001];
int ans;
int main() {
// freopen("explore3.in","r",stdin);
// freopen("explore.out","w",stdout);
cin >> t;
while (t--) {
memset(flag,0,sizeof flag);
ans = 1;
cin >> n >> m >> k;
cin >> x >> y >> d;
int nowx = x, nowy = y;
flag[nowx][nowy] = 1;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> mp[i][j];
}
}
while (k > 0) {
if (d == 0) {
k--;
nowx = nowx, nowy = nowy + 1;
if (nowy > m || nowy < 1) {
// k--;
d = (d + 1) % 4;
nowy = nowy - 1;
// continue;
}
if (mp[nowx][nowy] == '.' && d == 0 && flag[nowx][nowy] == 0) {
// k--;
ans++;
flag[nowx][nowy] = 1;
}
if (mp[nowx][nowy] == 'x' && d == 0) {
d = (d + 1) % 4;
nowy = nowy - 1;
}if(k == 0) break;
}
if (d == 1) {
k--;
nowx = nowx + 1, nowy = nowy;
if (nowx > n || nowx < 1) {
// k--;
d = (d + 1) % 4;
nowx = nowx - 1;
// continue;
}
if (mp[nowx][nowy] == '.' && d == 1 && flag[nowx][nowy] == 0) {
// k--;
ans++;
flag[nowx][nowy] = 1;
}
if (mp[nowx][nowy] == 'x' && d == 1) {
// k--;
d = (d + 1) % 4;
nowx = nowx - 1;
}
if(k == 0) break;
}
if (d == 2) {
k--;
nowx = nowx, nowy = nowy - 1;
if (nowy < 1 || nowy > m) {
// k--;
d = (d + 1) % 4;
nowy = nowy + 1;
// continue;
}
if (mp[nowx][nowy] == '.' && d == 2 && flag[nowx][nowy] == 0) {
// k--;
ans++;
flag[nowx][nowy] = 1;
}
if (mp[nowx][nowy] == 'x' && d == 2) {
// k--;
d = (d + 1) % 4;
nowy = nowy + 1;
}
if(k == 0) break;
}
if (d == 3) {
k--;
nowx = nowx - 1, nowy = nowy;
if (nowx < 1 || nowx > n) {
// k--;
d = (d + 1) % 4;
nowx = nowx + 1;
// continue;
}
if (mp[nowx][nowy] == '.' && d == 3 && flag[nowx][nowy] == 0) {
// k--;
ans++;
flag[nowx][nowy] = 1;
}
if (mp[nowx][nowy] == 'x' && d == 3) {
// k--;
d = (d + 1) % 4;
nowx = nowx + 1;
}
if(k == 0) break;
}
}
cout << ans << endl;
}
return 0;
}
by programmer330 @ 2024-10-26 23:05:22
@rwhy
已关.
但我只想知道为什么
by Ahws_rwhy @ 2024-10-26 23:07:16
@programmer330 你的读写错了