摘要:hdoj 1428漫步校园先放缩,求出没点离终点的最短距离。然后从起点bfs求出总的路径数,或者记忆化搜索求路径数。#include <iostream>#include <queue>using namespace std;typedef __int64 lld;struct Node { int x, y; Node() {} Node(int a, int b) { x = a; y = b; }};struct Node2 { int x, y; lld v;};bool operator<( Node2 n1, Node2 n2 ) { return n
阅读全文
摘要:hdoj 1226 超级密码求一个大数,满足是n的倍数,但是长度不超过500位。一开始以为搜索的状态有m^500,觉得无从下手。其实我们可以运用同模定理,所以状态最多是n,从0到n-1。还有这里要保存路径,所以用数组来模拟队列,每个节点记录他父亲节点在数组队列中的下标值。总结:1.大数问题,特别是涉及到倍数方面的,考虑下取模运算。 2.bfs搜索求路径时,用数组模拟队列取代queue。#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using na
阅读全文
摘要:hdoj 1180 诡异的楼梯规范的BFS写法,可以很有效地避免一些状态判重和状态转移上的细节错误,以下总结了三点。//hdoj 1180#include <iostream>#include <cstdio>#include <queue>using namespace std;struct Node { int x, y, step;};bool operator< ( Node n1, Node n2 ) { return n1.step > n2.step;}int n, m;int dir[4][2] = { 0, 1, 1, 0, 0
阅读全文
摘要:这题一边听着歌,一边做,结果相当悲剧。WA了无数次。。。。看来BFS写得还是要规范点,这样细节上才能减少出错。#include <iostream>#include <cmath>#include <queue>using namespace std;struct Position { int x, y;};struct Status { Position person, box; int deep;}_s;int mp[10][10], n, m;int dir[4][2] = { 1, 0, 0, -1, -1, 0, 0, 1 };Status per
阅读全文
摘要:hdoj 1455Sticks这题对dfs效率要求比较高,起初乱七八糟的代码果断TLE了。一般来说,dfs时能判断的尽量在上层进行判断,不要带到下层去判断。而且在这题中,如果相连的同样长的小棒不再同样搜索,这点很重要~~~~~~~#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>using namespace std;int num[70], n, g_sum, len;bool mark[70];bool dfs( int deep, int po
阅读全文
摘要:hdoj 1426 Sudoku Killer数独#include <iostream>using namespace std; struct Node { int x, y;}ns[85]; int cntSum, ca=1;int mp[10][10]; bool init() { int i, j; char ch[5]; cntSum = 0; for( i=0; i<9; ++i ) { for( j=0; j<9; ++j ) { if( scanf( "%s", ch ) == EOF ) return 0; ...
阅读全文
摘要:hdoj 1401Solitaire棋盘上有四个棋子,给你两个状态,问可否8步内将一个状态转移到另一个状态。双向BFS。偶这题写了一晚加一个下午,一开始兴致勃勃地单向BFS,当TLE后才发现状态总数估计错误了,应该是(4*4)^8。以后这样的错误要避免撒~~~~然后判重时用 int mark[8][8][8][8][8][8][8][8],一交,超内存了,再次悲剧。其实这样的话内存耗费是(8^8)*4/1024 = 65536K,明显要超的~~~~~ #include <iostream>#include <queue>#include <algorithm>
阅读全文
摘要:1.hdoj 1728 逃离迷宫这题本来用优先队列做的,WA很多次。网上看到下面的解释:举个例子,到达某个点的时候方向为向上或者向右的最少转弯次数都是4次假如终点在向上的方向,那么总的最少转弯次数就是4次但是开2维数组有可能造成记录的是向右的状态,因为同是4次。那么这样到达终点就需要5次转弯了。 代码2.hdoj 1242 Rescue以Angel为出发点,找出“距离”最近...
阅读全文