台州OJ 3709: Number Maze (数组越界不报RE,报WA坑爹)
http://acm.tzc.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=3709
You are playing one game called "Number Maze". The map of an example is shown in the following figure.

In the map, there are N*N+2 cells. When the game starts, you stay the top-left cell and you target is to reach the bottom-right cell with fewest number of moves. At the first step, you must move to the right of the start cell. After that, you can move to any cell (left, right, up or down, but can not move diagonally) if the target cell can be divided by the sum of the previous two numbers. However, you should never move backwards. For example, at first, you stay at the "2" cell, and you must move to the "6" cell, then have two selections "8" or "4" because (2+6)/8=1 and (2+6)/4=2, you can not move back to the "2" cell at this step although (2+6)/2=4. One possilbe solution is 2->6->8->7->5->3->4->7->11->2->13, and the total number of moves is 10.
Another solution is also legal but has longer moves:
2->6->8->7->5->3->4->7->5->3->4->7->11->2->13
Input
Thare are at most 20 cases. The first line of each case has three integers N<=10, S and T, which N indicates the dimension of the map, S and T indicate the number in the start and target cell. Then follows N lines and each line has N positive integers which indicate each number in the N*N cells.
There has one blank line after each case and you can assume that the total number of all cells is no larger than 1000000.
The inputs are ended with End of File. If you have some questions, please visit the help page.
Output
Each case outputs the fewest number of moves or "Impossible" if you can not reach the target cell per line.
Sample Input
3 2 13
6 4 3
8 7 5
2 11 2
Sample Output
题意:
给出一张这样的地图,由左上角的2开始,目标是走到右下角的13,走的时候,要求:1-->2,当2-->3的时候,要求1和2两个值相加的和能整除3那个点的值,并且不能返回走,就是走去3了后,就不能回到2了。(这个条件是很重要的,不然有bug)
思路:很简单,直接bfs,同时记录一个fa值,表示这个点是从那个点走过来的,就可以了。
然后做的时候无限wa,然后我就把路径输出来了,和样例一模一样,我觉得没可能错的啊。然后就想了一组数据,是死循环的数据,我没判断。然后我的做法是,对于一个10*10的数组,虽然可以重复走一些步数,但是扩展步数也有限的,900步,还要继续走就是死循环了,改了后居然过了。原来一开始我的数组开得太小了,才100,越界后,修改了其它内存,然后没报RE而是wa。找了很久的bug。唉。。但是我居然发现网上的代码,过不了我这组死循环数据(网上代码RE),然后那个代码是AC的。~~~~~!!!!!!
3 1 100
1 1 1
1 1 1
1 1 1
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define inf (1<<28)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <conio.h>
int n,s,t;
const int maxn=;
int a[maxn][maxn];
struct data
{
int x,y;
int count;
int fa;
};
int next[][]={{,},{,},{,-},{-,}};
int bfs (int bx,int by,int endx,int endy)
{
struct data que[*+]={};//这个数组开大点吧
int head,tail;
head=tail=;
int flag=;
que[].fa=-;
que[].x=bx;
que[].y=by;//开始的做上交那个格子 que[tail].count=;//第一步是固定的,从左上角那个走过来
que[tail].fa=;
que[tail].x=;
que[tail].y=;
tail++; flag++;//标记走了几步,防止死循环
while (head<tail)
{
for (int i=;i<;i++)
{
flag++;
if (flag==)//没可能走900步那么多的
{
return inf;
}
int tx=que[head].x+next[i][];
int ty=que[head].y+next[i][];
//(tx==que[que[head].fa].x && ty==que[que[head].fa].y
//判断不能返回走,必须判断
if (tx>=&&tx<=n+&&ty>=&&ty<=n+&&!(tx==que[que[head].fa].x && ty==que[que[head].fa].y))
{
int x=que[que[head].fa].x;
int y=que[que[head].fa].y;
int add=a[que[head].x][que[head].y]+a[x][y];
if (add%a[tx][ty]==)
{
//printf ("%d %d %d\n",add,tx,ty);
//getch();
que[tail].x=tx;
que[tail].y=ty;
que[tail].fa=head;
que[tail].count=que[head].count+;
if (tx==endx && ty==endy)
{
return que[tail].count;
}
tail++;
}
}
}
head++;
}
return inf;
}
void work ()
{
for (int i=;i<=maxn-;i++)
{
for (int j=;j<=maxn-;j++)
{
a[i][j]=inf;
}
}//全部设置为inf
//我把整个地图右下移动了一格
a[][]=s;
for (int i=;i<=n+;i++)
{
for (int j=;j<=n+;j++)
{
scanf ("%d",&a[i][j]);
}
}
a[n+][n+]=t;
/*
for (int i =1;i<=n+2;i++)
{
for (int j=1;j<=n+2;j++)
{
printf ("%10d ",a[i][j]);
}
printf ("\n");
}*/
int ans=bfs (,,n+,n+);
if (ans==inf)
{
printf ("Impossible\n");
}
else printf ("%d\n",ans);
return ; } int main ()
{
#ifdef local
freopen("data.txt","r",stdin);
#endif
while (scanf ("%d%d%d",&n,&s,&t)!=EOF)
{
work ();
}
return ;
}
虽然是过了,回想一下,还有一个地方值得思考,那就是,为什么不能回头走。刚开始的时候我还以为这里我判断错了,然后又造不出回头走wa的数据,现在用严格的数学证明下,回头走是会wa的。

假设我们走到了Y,然后(X+Y)%targe!=0 ① (就是说在Y那个格子,从X走过来的话,不能到达目标)
但是能走到Z ==> (X+Y)%Z==0 ②
假设,假设,我们能回头走,走回Y,有(Z+Y)%Y==0 ③
然后,我们能达到目标了(这样会wa了吧) (Z+Y)%targe==0
现在我们求解一组X,Y,Z
由②式有,X+Y=K1*Z; ==>(结合下面的) X=(K1*K2-K1-1)Y;
由③式有,Z+Y=K2*Y; ==> Z=(K2-1)*Y;
取K1=7 K2=3
解出
X=13Y
Y=Y
Z=2Y
那么我们的targe是3Y即可
所以,不能返回走是必要的,有数据坑你的
本人初出茅庐,如果有哪里错误的地方,还请读者多多指出,本人感激不尽!
台州OJ 3709: Number Maze (数组越界不报RE,报WA坑爹)的更多相关文章
- TZOJ 3709:Number Maze(广搜记录前驱)
描述 You are playing one game called "Number Maze". The map of an example is shown in the fo ...
- java.sql.SQLException之数组越界
java.sql.SQLException之数组越界 1.具体错误如下: (1)java.sql.SQLException:Parameter index out of range(0<1) ( ...
- iOS如何彻底避免数组越界
我们先来看看有可能会出现的数组越界Crash的地方: ? 1 2 3 4 5 6 7 - (void)tableView:(UITableView *)tableView didSelectRowAt ...
- Objective-c防止数组越界而崩溃(全局效果)
数组越界其实是很基本的问题,但是解决起来除了count的判断,还有每个调用的时候都要去判断一遍 对于不明确的数据总会有崩溃的风险 然而 每次调用都判断 那是太累了 so ..runtime&c ...
- 数组越界保护与消息传递black机制
数组越界保护if(index.row <= [array count]) 发送消息[[NSNotificationCenter defaultCenter] postNotificati ...
- 解决Android时时更新listview数组越界问题
时时更新数据一般出现在金融.股票行业对数据的准确性要求极高情况下使用. 先来看看下面一段代码, public class MainActivity extends Activity { private ...
- Android 【问题汇总】列表数组越界的问题
遇到了一个诡异的问题,ListView发生数组越界(偶尔会),程序崩溃. 错误信息如下: W/dalvikvm( ): threadid=: thread exiting with uncaught ...
- iOS 数组越界 Crash加工经验
我们先来看看有可能会出现的数组越界Crash的地方. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSInd ...
- Java中的数组越界问题
Java中数组初始化和OC其实是一样的,分为动态初始化和静态初始化, 动态初始化:指定长度,由系统给出初始化值 静态初始化:给出初始化值,由系统给出长度 在我们使用数组时最容易出现的就是数组越界问题, ...
随机推荐
- 51nod 1450 闯关游戏——期望dp
题目:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1450 想了半天,不知道不能走的状态(即最后不足m个的状态)怎么办. ...
- ECMAScript中面向对象的程序设计思想总结
<JavaScript高级程序设计>Chapter6笔记 1.ECMAScript内部值属性:数据属性和访问器属性 1)数据属性 数据属性的4个特性: configurable:表示能否通 ...
- 【转】 Pro Android学习笔记(五十):ActionBar(3):搜索条
目录(?)[-] ActionBar中的搜索条 通过Menu item上定义search view 进行Searchable的配置 在activity中将search view关联searchable ...
- 【转】Pro Android学习笔记(十六):用户界面和控制(4):ImageView控件
目录(?)[-] XML片段 代码设置ImageView ImageView是基础的控件,它是android.widget.ImageView的继承类. XML片段 <LinearLa ...
- DTP模型之一:(XA协议之一)XA协议、二阶段2PC、三阶段3PC提交
XA协议 XA是一个分布式事务协议,由Tuxedo提出.XA中大致分为两部分:事务管理器和本地资源管理器.其中本地资源管理器往往由数据库实现,比如Oracle.DB2这些商业数据库都实现了XA接口,而 ...
- hibernate 数据关联一对一
第一种一对一 person和card,card的id即作为主键,又作为外键 // 各村对方的一个对象 public class Person { private Integer id; privat ...
- Ubuntu 12.04 LTS为例演示vsftpd虚拟用户 的配置
vsftpd虚拟用户 2012-05-19 15:46:59| 分类: GNU/Linux | 标签:ubuntu vsftpd ftp 虚拟用户 |举报|字号 订阅 我们登录FTP有 ...
- cocos2dx之tolua++全面分析(二):类注册
tolua被作为库使用时,首先会做大量内部初始化工作: 一.tolua_open是入口点,它创建很多用于管理的内部变量,以下用_G指代全局表,_R指定registry table: 1._R.TOLU ...
- p2590&bzoj1036 树的统计
传送门(洛谷) 传送门(bzoj) 题目 一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w.我们将以下面的形式来要求你对这棵树完成一些操作: I. CHANGE u t : 把结点u的权值 ...
- 解决因为终端打印造成的java程序假死
问题状态: java 程序 日志采用 log4j 运行时由另一个管理进程拉起,程序在后台运行. 现象: 程序后台运行时,运行一段时间后假死 分析原因: 尝试打印输出,定位假死的具体位置,发现出现假死的 ...