zcmu——2100兰顿蚂蚁(模拟)

题目链接:

需要求得是行和列呀

不是横纵坐标

为啥这么累

#include<iostream>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#include<vector>
#include<map>
#include<algorithm>
#include<cstdio>
using namespace std;
int a[1010][1010];
int xx,yy;
int dir;
void change(int kk)
{
  if(dir==1)
	{
		if(kk)
		{
			dir=4;
			yy++;
		}
		else
		{
			dir=3;
			yy--;
		}
	}
	else
		if(dir==2)
		{
			if(kk)
			{
				dir=3;
				yy--;
			}
			else
			{
				dir=4;
				yy++;
			}
		}
		else
			if(dir==3)
			{
				if(kk)
				{
					dir=1;
					xx--;
				}
				else
				{
					dir=2;
					xx++;
				}
			}
			else
				if(dir==4)
				{
					if(kk)
					{
						dir=2;
						xx++;
					}
					else
					{
						dir=1;
						xx--;
					}
				}
}
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        {
            scanf("%d",&a[i][j]);
        }
    }
    int k;
    char s;
    scanf("%d %d %c %d",&xx,&yy,&s,&k);
    if(s=='U')
        dir=1;
    else if(s=='D')
        dir=2;
    else if(s=='L')
        dir=3;
    else if(s=='R')
        dir=4;
    while(k--)
    {
        int xxx=xx;
        int yyy=yy;
        change(a[xx][yy]);
        if(a[xxx][yyy])
            a[xxx][yyy]=0;
        else
            a[xxx][yyy]=1;
    }
    printf("%d %d\n",xx,yy);
    }
    return 0;
}

更简便的方法:

#include <cstdio> 
#include <cstdlib> 
#include <cstring> 
#include <cmath> 
#include <cstdlib> 
#include <map> 
#include <list> 
#include <vector> 
#include <stack> 
#include <queue> 
#include <algorithm> 
#include <iostream> 
#define go(i,a,b) for(int i=a;i<=b;i++) 
#define og(i,a,b) for(int i=a;i>=b;i--) 
#define mem(a) memset(a,0,sizeof(a)) 
using namespace std; 
main() 
{ 
    int m,n,x,y,k; char s; 
    int a[105][105]; 
    char dir[5]; 
    dir[0]='U';dir[1]='R';dir[2]='D';dir[3]='L'; 
    while(~scanf("%d%d",&m,&n)) 
    { 
        go(i,0,m-1) 
           go(j,0,n-1) 
              scanf("%d",&a[i][j]); 
        scanf("%d %d %c %d",&x,&y,&s,&k); 
        int t; 
        go(i,0,3) if(s==dir[i]) t=i; 
        go(i,1,k) 
        { 
            if(a[x][y]) t=(t+1)%4,a[x][y]=0; 
            else t=(t+3)%4,a[x][y]=1; 
            s=dir[t]; 
            if(s=='U') x--; 
            else if(s=='D') x++; 
            else if(s=='L') y--; 
            else y++; 
        } 
        printf("%d %d\n",x,y); 
    } 
} 

猜你喜欢

转载自blog.csdn.net/qq_42232118/article/details/82985840