zoj 1148 The Game 一个晚上终于AC!

这题。。。一开始理解错了,以为一个格子就算一步,看样例里第二个输出是3以为答案错了、、、了解真相前又修修改改让输出和样例一样。参考了别人的思路才发现原来要输出的是线段数,而不是格子数。。。


总结一下要注意的地方:

1. 我前面说的,惨痛教训。。。。

2.他可以往边上出去一格,边缘要特别处理一下

3.用一个二维b数组记录是否已走过这个格子,防止走回头路

4.用广搜的时候要一条路走到头,这样才能实现同一条线段上是一样的步数

5.循环输入,每个输出要编号,每张图之间空一行


痛定思痛,理解题意是关键,仔细思考样例的图和输出,看看它到底是怎么算步数的。


#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <vector> 
#include <functional>
#include <queue>
using namespace std;

typedef struct
{
	int x, y, step;
}block;

queue<block > q;

int main()
{
	int w, h, board=1;
	char a[80][80];
	int b[80][80];
	while(scanf("%d%d",&w,&h)&&w)
	{
		
		int pair=1;
		
		getchar();
		int i, j, x1,y1,x2,y2;
		memset(a, 0, sizeof(a));
		for(i=1; i<=h; i++)
		{
			for(j=1; j<=w; j++)
			{
				scanf("%c", &a[j][i]);
			}
			getchar();
		}
		printf("Board #%d:\n", board++);
		block t, tt;
		while(scanf("%d%d%d%d",&x1,&y1,&x2,&y2)&&x1)
		{
			memset(b, 0, sizeof(b));
			while(!q.empty())
				q.pop();
			int flag=0;
			t.x=x1;
			t.y=y1;
			t.step=0;
			q.push(t);
			while(!q.empty())
			{
				t=q.front();
				if((t.x==x2&&t.y==y2) || (t.x==x2&&t.y==y2) || (t.x==x2&&t.y==y2) || (t.x==x2&&t.y==y2))
				{
					printf("Pair %d: %d segments.\n", pair++, t.step);
					flag=1;
					break;
				}
				q.pop();
				if(t.x+1<=w+1&&(a[t.x+1][t.y]==' ' || a[t.x+1][t.y]==0) && !b[t.x+1][t.y] || (t.x+1==x2&&t.y==y2))
				{
					tt.x=t.x+1; tt.y=t.y; tt.step=t.step+1;		
					b[tt.x][tt.y]=1;
					q.push(tt);
					while(tt.x+1<=w+1&&(a[tt.x+1][tt.y]==' ' || a[tt.x+1][tt.y]==0) && !b[tt.x+1][tt.y] || (tt.x+1==x2&&tt.y==y2))
					{
						tt.x=tt.x+1;
					    b[tt.x][tt.y]=1;
					    q.push(tt);
					}
					
				}
				if(t.x-1>=0&&(a[t.x-1][t.y]==' ' || a[t.x-1][t.y]==0) && !b[t.x-1][t.y] || (t.x-1==x2&&t.y==y2))
				{
					tt.x=t.x-1; tt.y=t.y; tt.step=t.step+1;
					b[tt.x][tt.y]=1;
					q.push(tt);
					while(tt.x-1>=0&&(a[tt.x-1][tt.y]==' ' || a[tt.x-1][tt.y]==0) && !b[tt.x-1][tt.y] || (tt.x-1==x2&&tt.y==y2))
					{
						tt.x=tt.x-1;
						b[tt.x][tt.y]=1;
						q.push(tt);
					}
				}
				if(t.y+1<=h+1&&(a[t.x][t.y+1]==' ' || a[t.x][t.y+1]==0) && !b[t.x][t.y+1] || (t.x==x2&&t.y+1==y2))
				{
					tt.x=t.x; tt.y=t.y+1; tt.step=t.step+1;
					b[tt.x][tt.y]=1;
					q.push(tt);
					while(tt.y+1<=h+1&&(a[tt.x][tt.y+1]==' ' || a[tt.x][tt.y+1]==0) && !b[tt.x][tt.y+1] || (tt.x==x2&&tt.y+1==y2))
					{
						tt.y=tt.y+1; 
						b[tt.x][tt.y]=1;
						q.push(tt);
					}
				}
				if(t.y-1>=0&&(a[t.x][t.y-1]==' ' || a[t.x][t.y-1]==0) && !b[t.x][t.y-1] || (t.x==x2&&t.y-1==y2))
				{
					tt.x=t.x; tt.y=t.y-1; tt.step=t.step+1;
					b[tt.x][tt.y]=1;
					q.push(tt);
					while(tt.y-1>=0&&(a[tt.x][tt.y-1]==' ' || a[tt.x][tt.y-1]==0) && !b[tt.x][tt.y-1] || (tt.x==x2&&tt.y-1==y2))
					{
						tt.y=tt.y-1;
						b[tt.x][tt.y]=1;
						q.push(tt);
					}
				}

			}
			if(!flag)
				printf("Pair %d: impossible.\n", pair++);

		}

		printf("\n");
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是ZOJ1626的C++ AC代码,使用了旋转卡壳算法: ```c++ #include <iostream> #include <cstdio> #include <algorithm> #include <cmath> #include <cstring> #define MAXN 100010 #define eps 1e-8 #define INF 1e20 using namespace std; struct point { double x,y; friend point operator -(point a,point b) { point res; res.x=a.x-b.x; res.y=a.y-b.y; return res; } friend bool operator <(point a,point b) { if(fabs(a.x-b.x)<eps) return a.y<b.y; return a.x<b.x; } friend double operator *(point a,point b) { return a.x*b.y-a.y*b.x; } friend double dis(point a,point b) { return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); } }a[MAXN],b[MAXN],st[MAXN]; int n; double ans=INF; int cmp(point a,point b) { double tmp=(a-b)*(a[1]-b); if(fabs(tmp)<eps) return dis(a,a[1])-dis(b,a[1])<0; return tmp>0; } int main() { while(~scanf("%d",&n) && n) { for(int i=1;i<=n;i++) scanf("%lf%lf",&a[i].x,&a[i].y); sort(a+1,a+n+1); int tot=0; for(int i=1;i<=n;i++) { while(tot>=2 && (st[tot]-st[tot-1])*(a[i]-st[tot])<0) tot--; st[++tot]=a[i]; } int k=tot; for(int i=n-1;i>=1;i--) { while(tot>k && (st[tot]-st[tot-1])*(a[i]-st[tot])<0) tot--; st[++tot]=a[i]; } tot--; for(int i=1;i<=tot;i++) b[i]=st[i]; int tmp=1; for(int i=2;i<=tot;i++) if(b[i].y<b[tmp].y) tmp=i; swap(b[1],b[tmp]); sort(b+2,b+tot+1,cmp); st[1]=b[1]; st[2]=b[2]; k=2; for(int i=3;i<=tot;i++) { while(k>1 && (st[k]-st[k-1])*(b[i]-st[k])<=0) k--; st[++k]=b[i]; } double ans=0; if(k==2) ans=dis(st[1],st[2]); else { st[k+1]=st[1]; for(int i=1;i<=k;i++) for(int j=1;j<=k;j++) ans=max(ans,dis(st[i],st[j])); } printf("%.2lf\n",ans/2); } return 0; } ``` 其中,结构体 `point` 表示二维平面上的一个点,包含了点的坐标和一些基本操作。函数 `cmp` 是旋转卡壳算法中的比较函数,按照点到起点的极角从小到大排序。在主函数中,先使用 Graham 扫描法求出点集的凸包,然后按照旋转卡壳的步骤,求出凸包上的最远点对距离作为最小直径。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值