Robot Cleaner I

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4092

C++版本一

题解:

1、有个东西叫%1d;

2、哈希坐标;

3、剪枝;

/*
*@Author:   STZG
*@Language: C++
*/
#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<deque>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
//#define DEBUG
//#define RI register int
#define endl "\n"
using namespace std;
typedef long long ll;
//typedef __int128 lll;
const int N=2000+100;
const int M=100000+10;
const int MOD=1e9+7;
const double PI = acos(-1.0);
const double EXP = 1E-8;
const int INF = 0x3f3f3f3f;
int t,n,m;
ll k,p,l,r,u,v;
int ans,cnt,flag,temp,sum;
int a,b,mp[N][N];
int vis[N][300];
char str[N],str1[N];
struct node{
    int x,y,step;
}tmp,start,front1;
int command(int x,int y){
    return 81*mp[x][y]+27*mp[x-1][y]+9*mp[x+1][y]+3*mp[x][y-1]+mp[x][y+1];
}
int main()
{
#ifdef DEBUG
	freopen("input.in", "r", stdin);
	//freopen("output.out", "w", stdout);
#endif
    //ios::sync_with_stdio(false);
    //cin.tie(0);
    //cout.tie(0);
    scanf("%d",&t);
    while(t--){
        scanf("%d%d",&n,&m);
        scanf("%d%d%lld",&a,&b,&k);
        scanf("%s",str);
        sum=0;
        for(int i=1;i<=n;i++){
            for(int j=1;j<=m;j++){
                scanf("%1d",&mp[i][j]);
                if(mp[i][j]==2)
                    sum++;
            }
        }
        start.x=a;
        start.y=b;
        start.step=0;
        memset(vis,-1,sizeof(vis));
        ans=0;
        front1=start;
        while(1){
            int opid=command(front1.x,front1.y);
            char op=str[opid];
            if(vis[(front1.x-1)*m+front1.y][opid]==ans)
                break;
            if(sum<=ans)
                break;
            vis[(front1.x-1)*m+front1.y][opid]=ans;
            tmp=front1;
            tmp.step++;
            if(tmp.step>k)
                break;
            //cout<<front1.x<<" "<<front1.y<<" "<<op<<" "<<mp[front1.x][front1.y]<<" "<<vis[front1.x][front1.y]<<endl;
            if(op=='U'){
                tmp.x--;
            }else if(op=='D'){
                tmp.x++;
            }else if(op=='L'){
                tmp.y--;
            }else if(op=='R'){
                tmp.y++;
            }else if(op=='P'){
                if(mp[front1.x][front1.y]==2){
                    mp[front1.x][front1.y]=0;
                    ans++;
                }
            }else if(op=='I'){
                break;
            }
            if(mp[tmp.x][tmp.y]==1){
                break;
            }
            front1=tmp;
        }
        printf("%d\n",ans);
    }

#ifdef DEBUG
	printf("Time cost : %lf s\n",(double)clock()/CLOCKS_PER_SEC);
#endif
    //cout << "Hello world!" << endl;
    return 0;
}

C++版本二

题解:注意标记,vis[i][j] i表示对坐标的哈希,j表示当前计算的x,值为之前得到多少汽油了,因为如果再次经过这个位置,两次经过的中间得到多汽油,并且改变了图中的2,所以要继续走,知道,走到一个位置,汽油数不增加,然后就是注意一些剪枝。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll; 
int n,m;
int a,b;
ll k;
char mp[2010][2010];
int vis[2100][255];
char step[255];
int main()
{
	int T;
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d%d",&n,&m);
		scanf("%d%d%lld",&a,&b,&k);
		scanf("%s",step+1);
		int sum=0;
		for(int i=0;i<=n*m;i++)
			for(int j=0;j<=250;j++)
				vis[i][j]=-1;
		
		for(int i=1;i<=n;i++)
		{
			scanf("%s",mp[i]+1);
			for(int j=1;j<=m;j++)
			{
				if(mp[i][j]=='2')
					sum++;
			}
		}
		int ans=0;
		int x;
		int xx,yy;
		int st=0;
		while(1)
		{
			x=81*(mp[a][b]-'0')+27*(mp[a-1][b]-'0')+9*(mp[a+1][b]-'0')+3*(mp[a][b-1]-'0')+(mp[a][b+1]-'0');
			if(vis[(a-1)*m+b][x]==ans) break;
			vis[(a-1)*m+b][x]=ans;
			x++;
		//	cout<<a<<" "<<b<<" "<<x<<" "<<step[x]<<" "<<endl;
			if(step[x]=='U') xx=a-1,yy=b;
			if(step[x]=='D') xx=a+1,yy=b;
			if(step[x]=='L') xx=a,yy=b-1;
			if(step[x]=='R') xx=a,yy=b+1;
			if(step[x]=='P') 
			{
				if(mp[a][b]=='2')
				{
					ans++;
					mp[a][b]='0';	
				} 
				if(ans>=sum) break;
				xx=a,yy=b;
			}
			if(step[x]=='I') 
			{
				xx=a,yy=b;
				break;
			}
			if(mp[xx][yy]=='1') break;
			st++;
			if(st==k) break;
			a=xx;
			b=yy;
		}
		printf("%d\n",ans);
		
	}
	return 0;
}
全部评论

相关推荐

10-12 19:08
666 C++
花开蝶自来_:技能:听动物叫,让雪豹闭嘴
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务