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 ...
随机推荐
- [GIF] GIF Loop Coder - Introduction
Introducing the program, GIF Loop Coder, which allows you to make looping animated gifs (and other t ...
- POJ 1201 Intervals(图论-差分约束)
Intervals Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 20779 Accepted: 7863 Descri ...
- Deep Learning for NLP 文章列举
Deep Learning for NLP 文章列举 原文链接:http://www.xperseverance.net/blogs/2013/07/2124/ 大部分文章来自: http://w ...
- How To Install Kernel 3.10 On Ubuntu, Linux Mint, Debian and Derivates
n this article I will show you how to install Linux Kernel 3.10 on Ubuntu 13.10 Saucy Salamander, Ub ...
- 两个常用的功能,将shp数据属性转成TXT和Excel(转)
//转成txt public static void ConvertTable2Txt(ITable pTable, string pFilePath) { int pIndex = 0 ...
- iOS之苹果和百度地图的使用
iOS中使用较多的3款地图,google地图.百度地图.苹果自带地图(高德).其中苹果自带地图在中国使用的是高德的数据.苹果在iOS 6之后放弃了使用谷歌地图,而改用自家的地图.在国内使用的较多的就是 ...
- mysql mac 上启动
launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mariadb.plist 中内容: <string>/usr/local/o ...
- RedHat6.1(64bit)安装JDK
今天在服务器上装JDK1.5,费了不少力气,记录下来以供参考 服务器安装的操作系统为Red Hat 6.1(x86) [123@123 bin]$ cat /etc/redhat-release Re ...
- Spring3 + Spring MVC+ Mybatis 3+Mysql 项目整合(注解及源码)
Spring3 + Spring MVC+ Mybatis 3+Mysql 项目整合(注解及源码) 备注: 之前在Spring3 + Spring MVC+ Mybatis 3+Mysql 项目整合中 ...
- 那天有个小孩跟我说LINQ(六)转载
2 LINQ TO SQL完结(代码下载) 我们还是接着上次那个简单的销售的业务数据库为例子,打开上次那个例子linq_Ch5 2.1 当数据库中的表建立了主外键 ①根据主键获取子表信息 ...