HDU 5612:Baby Ming and Matrix games 分数模板
2016-01-24 12:31
405 查看
Baby Ming and Matrix games
Time Limit: 2000/1000 MS (Java/Others)Memory Limit: 65536/65536 K (Java/Others)
问题描述
铭宝宝喜欢玩游戏,这两天他喜欢玩下面这个游戏了。 给出一个n*mn∗m的矩阵,矩阵的(i*2,j*2)(i∗2,j∗2) (其中i, j = 0, 1, 2...i,j=0,1,2...) 位置上为0~9的数字,矩阵中每两个数字中间有一个算术符号(+、-、*、/),其他位置用#填充。 问题是:是否能在上述矩阵中,找到一个表达式,使得表达式的计算结果为给出的sum(表达式从左到右计算) 表达式按照如下方式获取:选择一个数字作为起点,每次选择相邻的数字XX进行一次计算,把得到的结果保存在数字XX上,并选择该位置为下一个起点(同一个位置的数字不能使用22次)。
输入描述
输入T(T \leq 1,000)T(T≤1,000)表示测试组数 输入33个数,奇数n,mn,m以及整数sumsum(除0的式子是不合法的,除法规则见样例,-10^{18} < sum < 10^{18}−1018<sum<1018) 接下来输入nn行,每行mm个字符,表示给出的矩阵(矩阵内的数字个数\leq 15≤15)
输出描述
输出Possible,表示能够找到这样的表达式 输出Impossible,表示不能够找到这样的表达式
输入样例
3 3 3 24 1*1 +#* 2*8 1 1 1 1 3 3 3 1*0 /#* 2*6
输出样例
Possible Possible Possible
Hint
第一组样例:1+2*8=24 第三组样例:1/2*6=3
题目一共最多15个数,深搜。这个题自己被精度卡的不能自理了。。。没办法,自己手写了一个分数。
代码:
#pragma warning(disable:4996) #include <iostream> #include <algorithm> #include <cstring> #include <cstdio> #include <vector> #include <string> #include <cmath> #include <queue> #include <map> using namespace std; typedef long long ll; const int maxn = 5005; ll gcd(ll a, ll b) { return a%b ? gcd(b, a%b) : b; } class Fraction { public: ll up;//分子 ll down;//分母 Fraction() { up = 0; down = 1; } Fraction(const ll k) { up = k; down = 1; } Fraction(const ll u, const ll d) { ll cc = gcd(u, d); up = u / cc; down = d / cc; } Fraction operator =(const Fraction &)const; Fraction operator +(const Fraction &)const; Fraction operator -(const Fraction &)const; Fraction operator *(const Fraction &)const; Fraction operator /(const Fraction &)const; bool operator ==(Fraction &)const; }; Fraction Fraction::operator=(const Fraction &T)const { Fraction t(*this); Fraction res; res.up = t.up; res.down = t.down; ll cc = gcd(res.up, res.down); res.up = res.up / cc; res.down = res.down / cc; return res; } Fraction Fraction::operator+(const Fraction &T)const { Fraction t(*this); Fraction res; res.up = t.up*T.down + t.down*T.up; res.down = t.down*T.down; ll cc = gcd(res.up, res.down); res.up = res.up / cc; res.down = res.down / cc; return res; } Fraction Fraction::operator-(const Fraction &T)const { Fraction t(*this); Fraction res; res.up = t.up*T.down - t.down*T.up; res.down = t.down*T.down; ll cc = gcd(res.up, res.down); res.up = res.up / cc; res.down = res.down / cc; return res; } Fraction Fraction::operator*(const Fraction &T)const { Fraction t(*this); Fraction res; res.up = t.up*T.up; res.down = t.down*T.down; ll cc = gcd(res.up, res.down); res.up = res.up / cc; res.down = res.down / cc; return res; } Fraction Fraction::operator/(const Fraction &T)const { Fraction t(*this); Fraction res; res.up = t.up*T.down; res.down = t.down*T.up; ll cc = gcd(res.up, res.down); res.up = res.up / cc; res.down = res.down / cc; return res; } bool Fraction::operator==(Fraction &T)const { Fraction t(*this); ll cc = gcd(t.up, t.down); t.up = t.up / cc; t.down = t.down / cc; cc = gcd(T.up, T.down); T.up = T.up / cc; T.down = T.down / cc; return t.up == T.up&&t.down == T.down; } Fraction temp; int des; ll n, m, sum; char x[3005], oper[50][50]; int dp[50][50], vis[50][50]; void input() { int i, j, len; scanf("%I64d%I64d%I64d", &n, &m, &sum); memset(dp, 0, sizeof(dp)); memset(oper, 0, sizeof(oper)); for (i = 1; i <= n; i++) { scanf("%s", x); if (i & 1) { for (j = 0; j < m; j++) { if (x[j] >= '0'&&x[j] <= '9') { dp[i][j + 1] = x[j] - '0'; } else { oper[i][j + 1] = x[j]; } } } else { for (j = 0; j < m; j++) { oper[i][j + 1] = x[j]; } } } } void dfs(int x, int y,Fraction &cur) { if (des == 1) { vis[x][y] = 0; return; } Fraction s(sum); if (cur == s) { des = 1; vis[x][y] = 0; return; } vis[x][y] = 1; if (x >= 3 && vis[x - 2][y] == 0) { Fraction temp(dp[x - 2][y]); if (oper[x - 1][y] == '*') { Fraction cur2 = cur*temp; dfs(x - 2, y, cur2); } else if (oper[x - 1][y] == '+') { Fraction cur2 = cur + temp; dfs(x - 2, y, cur2); } else if (oper[x - 1][y] == '-') { Fraction cur2 = cur - temp; dfs(x - 2, y, cur2); } else if (oper[x - 1][y] == '/') { if (dp[x - 2][y] != 0) { Fraction cur2 = cur / temp; dfs(x - 2, y, cur2); } } } if (y >= 3 && vis[x][y-2] == 0) { Fraction temp(dp[x][y - 2]); if (oper[x][y-1] == '*') { Fraction cur2 = cur* temp; dfs(x, y - 2, cur2); } else if (oper[x][y - 1] == '+') { Fraction cur2 = cur + temp; dfs(x, y - 2, cur2); } else if (oper[x][y - 1] == '-') { Fraction cur2 = cur - temp; dfs(x, y - 2, cur2); } else if (oper[x][y - 1] == '/') { if (dp[x][y - 2] != 0) { Fraction cur2 = cur / temp; dfs(x, y - 2, cur2); } } } if (x <= n-2 && vis[x + 2][y] == 0) { Fraction temp(dp[x + 2][y]); if (oper[x + 1][y] == '*') { Fraction cur2 = cur*temp; dfs(x + 2, y, cur2); } else if (oper[x + 1][y] == '+') { Fraction cur2 = cur + temp; dfs(x + 2, y, cur2); } else if (oper[x + 1][y] == '-') { Fraction cur2 = cur - temp; dfs(x + 2, y, cur2); } else if (oper[x + 1][y] == '/') { if (dp[x + 2][y] != 0) { Fraction cur2 = cur / temp; dfs(x + 2, y, cur2); } } } if (y <= m - 2 && vis[x][y+2] == 0) { Fraction temp(dp[x][y + 2]); if (oper[x][y + 1] == '*') { Fraction cur2 = cur*temp; dfs(x, y + 2, cur2); } else if (oper[x][y + 1] == '+') { Fraction cur2 = cur + temp; dfs(x, y + 2, cur2); } else if (oper[x][y + 1] == '-') { Fraction cur2 = cur - temp; dfs(x, y + 2, cur2); } else if (oper[x][y + 1] == '/') { if (dp[x][y + 2] != 0) { Fraction cur2 = cur / temp; dfs(x, y + 2, cur2); } } } vis[x][y] = 0; } void solve() { int i, j; des = 0; for (i = 1; i <= n; i = i + 2) { for (j = 1; j <= m; j = j + 2) { memset(vis, 0, sizeof(vis)); temp.up=dp[i][j]; temp.down = 1; dfs(i, j, temp); if (des == 1) { puts("Possible"); return; } } } puts("Impossible"); return; } int main() { //freopen("i.txt", "r", stdin); //freopen("o.txt", "w", stdout); int t; scanf("%d", &t); while (t--) { input(); solve(); } return 0; }
相关文章推荐
- IO流(Properties存取)
- mfs 连接频繁断开
- win10、win7系统64位oracle11g安装教程以及32位plsql连接教程
- 李航《统计学习方法》读书笔记(1):朴素贝叶斯分类
- 基础知识(3)- Java的基本程序设计结构
- C++中函数如何返回数组
- FFMpeg提取视频帧
- 我的myeclipse+maven
- UIImageView的两种创建方式的比较
- Palindrom Numbers
- 76. Binary Tree Inorder Traversal
- (入门)uva 1368 DNA Consensus String
- 11g RAC and DG
- ZOJ11024
- C++中的四舍五入
- c++实现atoi()和itoa()函数(字符串和整数转化)
- 纵横字谜的答案(uva 232)
- N!结尾0的个数
- uva11292 - The Dragon of Loowater (贪心)
- Android开发之如何保证Service不被杀掉(broadcast+system/app)