SGU 126

简单的BFS。无需任何优化。

利用一个结构体数组储存状态,三个量a,b,move分别表示A箱,B箱的球数以及移动次数。

注意对特殊情况的处理以及对不可能情况的判定:

(1)两数之差为奇数,由题意,假设a<b,则移动球数为a,差的改变量为2a。

又有最终状态的前一步是两箱数目相等,差=0,所以不可能实现。

(2)两数之差模4余2,但 初始球数都为偶数。

根据上面,类似地可以推导出不可能。

(3)出现和前面相同的情况。

- -这个就比较难处理- -数据范围在那里- - 总不能开个VIS[2^31][2^31]吧- -

一种偷懒的方法(也是苯渣用的方法)是限定一个遍历的情况数,我设的150000.

所幸SGU的数据比较弱- -(或者任一种状况都会在150000之前发生重复?未可知)

附代码:

#include "stdio.h"
#include "string.h"
#include "stdlib.h"
const int MAXMOVE=150000;
struct status{
	int a,b,move;
};
int BFS(int A,int B){
	status q[MAXMOVE+10];
	int flag=0,i,j,front,rear,tmp;
	front=0;rear=0;
	q[0].a=A;q[0].b=B;
	q[0].move=0;
	if(!q[0].a||!q[0].b){
		return 0;
	}
	if((q[0].a+q[0].b)%2){
		return -1;
	}
	else{
		while(1){
			if((q[0].a%2==0)&&((q[0].a-q[0].b)%4)){
				return -1;
				break;
			}
			if(rear>MAXMOVE){
				return -1;
				break;
			}
			//printf("Front=%d,Rear=%d.\n",front,rear);
			tmp=rear;
			while(front<=tmp){
				//printf("***front=%d.\n",front);
				//Move Balls from B To A.
				if(q[front].b>=q[front].a){
					rear++;
					q[rear].a=2*q[front].a;
					q[rear].b=q[front].b-q[front].a;
					//printf("******Rear = %d:  A = %d, B = %d.\n",rear,q[rear].a,q[rear].b);
					//getchar();
					q[rear].move=q[front].move+1;
					if(!q[rear].a||!q[rear].b){
						return q[rear].move;
						flag=1;
						break;
					}
				}
				//Move Balls from A To B.
				if(q[front].a>=q[front].b){
					rear++;
					q[rear].b=2*q[front].b;
					q[rear].a=q[front].a-q[front].b;
					q[rear].move=q[front].move+1;
					//printf("*******Rear = %d:  A = %d, B = %d.\n",rear,q[rear].a,q[rear].b);
					//getchar();
					if(!q[rear].a||!q[rear].b){
						return q[rear].move;
						flag=1;
						break;
					}
				}
				front++;
			}
			if(flag)break;
		}
	}
}
int main(){
	int ans,A,B;
	scanf("%d%d",&A,&B);
	ans=BFS(A,B);
	printf("%d\n",ans);
	return 0;
}

SGU 126,布布扣,bubuko.com

时间: 2024-07-30 20:24:00

SGU 126的相关文章

Boxes - SGU 126(找规律)

题目大意:有两个箱子,一个箱子装了A个球,一个箱子装了B个球,可以从球多的那个箱子拿出来球少的箱子里面球的总数放在少的那个箱子里面,问能否把球全部放在一个箱子里面? 分析:很容易求出来最后放的拿一下一定是A == B,再继续往前推A/=2....所以判断拿球的过程中是否有2^k次方数,如果有就能放,没有肯定不能放,特殊的AB开始有为0的,直接模拟也可以,注意两个箱子大小交替不会超过两次,如果超过,一定不能交换成功,因为如果能摆放成功的话,最多交换一次. 代码如下: ===============

SGU 438 The Glorious Karlutka River =) 拆点+动态流+最大流

The Glorious Karlutka River =) Time Limit:500MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice SGU 438 Appoint description: Description A group of Mtourists are walking along the Karlutka river. They want to cross

sgu Kalevich Strikes Back

这道题就是求一个大矩形被n个矩形划分成n+1个部分的面积,这些矩形之间不会相交,可能包含.. 1 #include <cstdio> 2 #include <cstring> 3 #include <vector> 4 #include <algorithm> 5 #define maxn 120100 6 using namespace std; 7 8 long long s[maxn]; 9 vector<int>g[maxn]; 10 i

数学计数原理(P&#243;lya,高精度):SGU 294 He&#39;s Circles

He's Circles He wrote n letters "X" and "E" in a circle. He thought that there were 2n possibilities to do it, because each letter may be either "X" or "E". But Qc noticed that some different sequences of letters ca

sgu Ice-cream Tycoon

题意:供应商提供n块价格为c的冰淇淋,一个学生想买n块冰淇淋,手中的钱数总共有t元,为了不让买n块冰淇淋所花费的钱数不超过t元,先尽可能卖给这个学生便宜的冰淇淋. 如果这个学生不能买到所需要的冰淇淋则输出“UNHAPPY”,能则输出“HAPPY”. 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #define maxn 200000 5 using namespace std; 6 7

sgu 463 - Walking around Berhattan

K - Walking around Berhattan Time Limit:250MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice SGU 463 Description As you probably know, Berhattan is a district of Berland's largest city and it consists of equal squar

老男孩教育每日一题-第126天-通过shell脚本打印乘法口诀表

问题背景: 生成9*9乘法表 [[email protected] ~]# seq 9 | sed 'H;g' | awk -v RS='' '{for(i=1;i<=NF;i++)printf("%dx%d=%d%s", i, NR, i*NR, i==NR?"\n":"\t")}' 1x1=1 1x2=2   2x2=4 1x3=3   2x3=6   3x3=9 1x4=4   2x4=8   3x4=12  4x4=16 1x5=5

【SGU 390】Tickets (数位DP)

Tickets Description Conductor is quite a boring profession, as all you have to do is just to sell tickets to the passengers. So no wonder that once upon a time in a faraway galaxy one conductor decided to diversify this occupation. Now this conductor

ACM: SGU 101 Domino- 欧拉回路-并查集

sgu 101 - Domino Time Limit:250MS     Memory Limit:4096KB     64bit IO Format:%I64d & %I64u Description Dominoes – game played with small, rectangular blocks of wood or other material, each identified by a number of dots, or pips, on its face. The bl