hdu 5612 Baby Ming and Matrix games
题意: 给一个矩形,两个0~9的数字之间隔一个数学运算符(‘+’,’-‘,’*’,’/’),其中’/’表示分数除,再给一个目标的值,问是否存在从一个数字出发,以数字之间的运算符为运算,得到这个目标值;(每个数字只能用一次,其实说白了就是dfs..);可以则输出(Impossible),否则输出(Possible);
思路:坑点就是里面本来全是整数,但是一个除法运算却是分数形式,开始使用了很保险的分数保存,来避免误差的。但是无情WA了很多次。。突然发现写成了(ImPossible)…醉了,然后以为现在肯定要A了,但是还是WA了。。。(有兴趣的帮我找找bug哈!真心感谢)之后重写了一个double类型的,无奈啊(太水了),下面是A和WA两份代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string.h>
#include<algorithm>
#include<map>
#include<queue>
#include<vector>
#include<cmath>
#include<stdlib.h>
#include<time.h>
using namespace std;
#define rep(i,n) for(int i = 1;i <= n;i++)
#define MS0(a) memset(a,0,sizeof(a))
#define esp 1e-6
int n,m,vis[][],f;
double sum;
char s[][];
int dir[][] = {{,,,-},{,,-,}};
double cal(double val,double v,char op)
{
if(op == '+') return val + v;
else if(op == '-') return val - v;
else if(op == '*') return val * v;
return val/v;
}
void dfs(int i,int j,double val)
{
if(fabs(val - sum) < esp) f = ;
for(int k = ;k < && f;k++){
int x = i + *dir[][k] , y = j + *dir[][k];
char op = s[i + dir[][k]][j + dir[][k]];
if(x < || x > n || y < || y > m || vis[x][y]) continue;
int v = s[x][y] - '';
if(op == '/' && v == ) continue;
vis[x][y] = ;
dfs(x,y,cal(val,v,op));
vis[x][y] = ;
}
}
int main()
{
int T,i,j;
cin>>T;
while(T--){
f= ;MS0(vis);
scanf("%d%d%lf",&n,&m,&sum);
rep(i,n) scanf("%s",s[i] + );
for(i = ;i <= n && f;i += )
for(j = ;j <= m && f;j += ){
vis[i][j] = ;
dfs(i,j,s[i][j] - '');
vis[i][j] = ;
}
puts(f?"Impossible":"Possible");
}
return ;
}
Accpeted code
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string.h>
#include<algorithm>
#include<map>
#include<queue>
#include<vector>
#include<cmath>
#include<stdlib.h>
#include<time.h>
using namespace std;
typedef long long ll;
#define MS0(a) memset(a,0,sizeof(a))
ll cal(ll a,ll b,char op)
{
if(op == '+') return a + b;
if(op =='-') return a - b;
if(op == '*') return a * b;
}
int dir[][] = {{,,,-},{,,-,}};
char s[][];
ll sum,f;
int n,m,vis[][];
void dfs(int i,int j,ll ele,ll den)
{
if(ele%den == && ele / den == sum) f = ;
ll e = ele, d = den;
for(int k = ;k < && f;k++){
//system("pause");
int x = i + *dir[][k],y = j + *dir[][k];
char op = s[i+dir[][k]][j+dir[][k]];
if(x < || x > n || y < || y > m||vis[x][y]) continue;
ele = e,den = d;
int val = (s[x][y]-'');
if(op =='/'){
if(val == ) continue;
if(ele % val) den *= val;
else ele /= val;
}
else ele = cal(ele,val,op);
if(ele % den == ) ele /= den,den = ;
vis[x][y] = ;
dfs(x,y,ele,den);
vis[x][y] = ;
}
}
int main()
{
int T,i,j;
cin>>T;
while(T--){
MS0(vis);
scanf("%d%d%I64d",&n,&m,&sum);
for(i = ;i <= n;i++)
scanf("%s",s[i] + );
f = ;
for(i = ;i <= n && f;i += )
for(j = ;j <= m && f;j += ){
vis[i][j] = ;
dfs(i,j,(ll)(s[i][j] - ''),);
vis[i][j] = ;
}
puts(f?"Impossible":"Possible");
}
return ;
}
WA Code
hdu 5612 Baby Ming and Matrix games的更多相关文章
- hdu 5612 Baby Ming and Matrix games(dfs暴力)
Problem Description These few days, Baby Ming is addicted to playing a matrix game. Given a n∗m matr ...
- HDU 5612 Baby Ming and Matrix games(DFS)
题目链接 题解:题意为给出一个N*M的矩阵,然后(i∗2,j∗2) (i,j=0,1,2...)的点处是数字,两个数字之间是符号,其他位置是‘#’号. 但不知道是理解的问题还是题目描述的问题,数据中还 ...
- Baby Ming and Matrix games(dfs计算表达式)
Baby Ming and Matrix games Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Ja ...
- hdu5612 Baby Ming and Matrix games (dfs加暴力)
Baby Ming and Matrix games Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Ja ...
- HDU 5614 Baby Ming and Matrix tree 树链剖分
题意: 给出一棵树,每个顶点上有个\(2 \times 2\)的矩阵,矩阵有两种操作: 顺时针旋转90°,花费是2 将一种矩阵替换为另一种矩阵,花费是10 树上有一种操作,将一条路经上的所有矩阵都变为 ...
- hdoj--5612--Baby Ming and Matrix games(dfs)
Baby Ming and Matrix games Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K ...
- hdu 5611 Baby Ming and phone number(模拟)
Problem Description Baby Ming collected lots of cell phone numbers, and he wants to sell them for mo ...
- hdu 5610 Baby Ming and Weight lifting
Problem Description Baby Ming is fond of weight lifting. He has a barbell pole(the weight of which c ...
- HDU 5613 Baby Ming and Binary image
因为第一行和最后一行都是0,我们只需枚举最左边或最右边一列的01情况,即可得到整张表 然后再检验表是否符合要求 #include<cstdio> #include<cstring&g ...
随机推荐
- Unity3D问题之EnhanceScollView选择角色3D循环滚动效果实现
需求 呈现3D效果(2D素材)选择角色效果 滚动保证层级.缩放比例.间距正常尾随 循环滚动 这个界面需求一般也会有游戏会採用(貌似有挺多) 怎样实现 实现技术关键点 (3D循环效果,依据数学函数和细致 ...
- window nginx 启动无提示错误,却没有listen 80port
一直使用虚拟机来使用web+hostonly方式; 今天为了測试一个php平台的window系统兼容性, 在官方下载了window-nginx 1.9.1版本号; 解压到文件夹, 执行nginx.ex ...
- python版的短信轰炸机smsbomb----------下篇(get)
在上一篇介绍的是post方式发送数据,可是有点站点是get方式发送数据,比如:http://www.oupeng.com/download,事实上方法差点儿相同. import httplib,url ...
- Winform-CheckBox、ChecklistBox控件遍历代码
CheckBox,是各种开发语言环境和网页中常用的控件.下面,本文给大家讲解的是C#Winform版的CheckBox.控件遍历.全选.反选实例代码.①直接引用如果窗体form中存在CheckBox控 ...
- Ⅶ.spring的点点滴滴--自定义对象行为
承接上文 自定义对象行为 .net篇(环境为vs2012+Spring.Core.dll v1.31) public class lifeCycle : Spring.Objects.Factory. ...
- Android中的Handler的机制与用法详解
概述: 很多android初学者对android 中的handler不是很明白,其实Google参考了Windows的消息处理机制, 在Android系统中实现了一套类似的消息处理机制.在下面介绍ha ...
- c语言,strcpy
#include <stdio.h> #include <string.h> int main() { char string[10]; char *str="a ...
- AWS 命令行界面 + Python 的 AWS 开发工具包 (Boto3)
安装AWS CLI $ pip install awscli 安装Boto3 $ pip install boto3 设置AWS CLI $ aws configure AWS Access Key ...
- ORA-01461: 仅能绑定要插入 LONG 列的 LONG 值
检查插入的值是否大于该字段数据类型约束的长度. 这个异常是指,用户向数据库执行插入数据操作时,某条数据的某个字段值过长,如 果是varchar2类型的,当长度超过2000,--4000(最大值)之间的 ...
- c#进程间通讯方案之IPC通道
转载:http://www.cnphp.info/csharp-ipc-channel-remoting.html 最近一直纠结与使用多进程还是多线程来构建程序.多线程的方法似乎不错,但是一个进程可承 ...