HDU 5612:Baby Ming and Matrix games 分数模板

3 篇文章 0 订阅

Baby Ming and Matrix games

 
 Time Limit: 2000/1000 MS (Java/Others)
 
 Memory Limit: 65536/65536 K (Java/Others)
问题描述
铭宝宝喜欢玩游戏,这两天他喜欢玩下面这个游戏了。
给出一个n*mnm的矩阵,矩阵的(i*2,j*2)(i2,j2) (其中i, j = 0, 1, 2...i,j=0,1,2...) 位置上为0~9的数字,矩阵中每两个数字中间有一个算术符号(+、-、*、/),其他位置用#填充。
问题是:是否能在上述矩阵中,找到一个表达式,使得表达式的计算结果为给出的sum(表达式从左到右计算)
表达式按照如下方式获取:选择一个数字作为起点,每次选择相邻的数字XX进行一次计算,把得到的结果保存在数字XX上,并选择该位置为下一个起点(同一个位置的数字不能使用22次)。
输入描述
输入T(T \leq 1,000)T(T1,000)表示测试组数
输入33个数,奇数n,mn,m以及整数sumsum(除0的式子是不合法的,除法规则见样例,-10^{18} < sum < 10^{18}1018<sum<1018)
接下来输入nn行,每行mm个字符,表示给出的矩阵(矩阵内的数字个数\leq 1515
输出描述
输出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;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值