程序设计实习MOOC / 程序设计与算法(二)测验汇总(2022秋季) 014-023

题目来源:openjudge

014:最佳加法表达式

总时间限制: 1000ms 内存限制: 65536kB

描述

给定n个1到9的数字,要求在数字之间摆放m个加号(加号两边必须有数字),使得所得到的加法表达式的值最小,并输出该值。例如,在1234中摆放1个加号,最好的摆法就是12+34,和为36

输入

有不超过15组数据
每组数据两行。第一行是整数m,表示有m个加号要放( 0<=m<=50)
第二行是若干个数字。数字总数n不超过50,且 m <= n-1

输出

对每组数据,输出最小加法表达式的值

样例输入
2
123456
1
123456
4
12345
样例输出
102
579
15
提示

要用到高精度计算,即用数组来存放long long 都装不下的大整数,并用模拟列竖式的办法进行大整数的加法。

来源

Guo Wei

一个参考解答

参考改进自博客园-郭怡柔
其中的solve函数,可行但会超时,,

#include<iostream>
#include<cstring>

using namespace std;

struct BigInt{
	int num[60];
	int len;
	BigInt(){
		len=1;
		memset(num,0,sizeof(num));
	}
	BigInt(char ch[], int s, int l){
		len=(l==0?1:l);memset(num,0,sizeof(num));
		for(int i=0;i<l;++i) num[l-i-1]=ch[s+i]-'0';
	}
	BigInt(int n){
		len=(n>0?0:1);memset(num,0,sizeof(num));
		while(n){
			num[len]=n%10;
			n /= 10;
			++len;
		}
	}
	BigInt operator+(const BigInt & n){
		int ml=max(len,n.len);
		int carry=0;
		BigInt res;
		for(int i=0;i<ml;++i){
			res.num[i] = carry + num[i] + n.num[i];
			if(res.num[i]>=10){
				res.num[i] -= 10;
				carry=1;
			} else { carry=0;}
		}
		if(carry){
			res.len=ml+1;
			res.num[ml]=1;
		} else {res.len=ml;}
		return res;
	}
	bool operator<(const BigInt & n){
		if(len<n.len) return true;
		else if(len>n.len) return false;
		else {
			for(int i=len-1;i>=0;--i){
				if(num[i]<n.num[i]) return true;
				else if(num[i]>n.num[i]) return false;
			}
			return false; // ==
		}
	}
};
ostream & operator<<(ostream & o, const BigInt & n){
	for(int i=n.len-1;i>=0;--i) o<<n.num[i];
	return o;
}

BigInt mINF;
char ch[52];
BigInt V[52][52];
BigInt NUM[52][52];

BigInt solve(int n, int m){
	if(m==0) return BigInt(ch, 0, n);
	else if(n<m+1) return mINF;
	else if(n==m+1) {
		int r=0;
		for(int i=0;i<n;++i) r += (ch[i]-'0');
		return BigInt(r);
	}
	else {
		BigInt res=mINF;
		for(int i=m;i<n-1;++i){
			BigInt tp =solve(i,m-1)+BigInt(ch,i,n-i);
			if(tp<res) res=tp;
			
		}
		return res;
	}
}

int main(){
	int n,m;
	mINF.len=51;
	mINF.num[50]=1;
	while(cin>>m){
		cin >> ch;
		n=strlen(ch);
		for(int i=1;i<=n;++i){
			for(int j=i;j<=n;++j){
				NUM[i][j] = BigInt(ch, i-1, j-i+1);
			}
		}
		for(int i=1;i<=n;++i){
			V[0][i] = BigInt(ch,0,i);
		}
		for(int i=1;i<=m;++i){
			for(int j=1;j<=n;++j){
				if(j<i+1) V[i][j]=mINF;
				else{
					BigInt tpMin = mINF;
					for(int k=i;k<j;++k){
						BigInt tp=V[i-1][k]+NUM[k+1][j];
						if(tp<tpMin) tpMin =tp;
					}
					V[i][j]=tpMin;
				}
			}
		}
		cout << V[m][n] << endl;
		// cout << solve(n, m) <<endl;
	}
	return 0;
}
015:复杂的整数划分问题

总时间限制: 200ms 内存限制: 65536kB

描述

将正整数n 表示成一系列正整数之和,n=n1+n2+…+nk, 其中n1>=n2>=…>=nk>=1 ,k>=1 。
正整数n 的这种表示称为正整数n 的划分。

输入

标准的输入包含若干组测试数据。每组测试数据是一行输入数据,包括两个整数N 和 K。
(0 < N <= 50, 0 < K <= N)

输出

对于每组测试数据,输出以下三行数据:
第一行: N划分成K个正整数之和的划分数目
第二行: N划分成若干个不同正整数之和的划分数目
第三行: N划分成若干个奇正整数之和的划分数目

样例输入
5 2
样例输出
2
3
3
提示

第一行: 4+1, 3+2,
第二行: 5,4+1,3+2
第三行: 5,1+1+3, 1+1+1+1+1+1

一个参考解答
#include<iostream>
#include<cstring>

using namespace std;

/* 
// Time Limit Exceeded
int solve(int n, int m){
	if(n<=0) return 0;
	if(n==1||m==1) return 1;
	int res=0;
	if(m>=n) res++;
	for(int i=m;i>1;--i){
		res += solve(n-i, i);
	}
	if(n>1) res++;
	return res;
}*/
int dp1[60][60][60]={0};
int dp2[60][60]={0};
int dp3[60][60]={0};
int sol1(int n, int m, int k){
	if(n<0 || m<=0) return 0;
	if(n==0){
		if(k==0) return 1;
		else return 0;
	}
	if(k==1) return (n<=m?1:0);
	if(dp1[n][m][k]!=-1)
		return dp1[n][m][k];
	int res=0;
	for(int i=m;i>=1;--i)
		res += sol1(n-i, i, k-1);
	dp1[n][m][k] = res;
	return res;
}

int sol2(int n, int m){
	if(n==0) return 1;
	if(n<0 || m<=0) return 0;
	if(dp2[n][m]!=-1)
		return dp2[n][m];
	int res=0;
	for(int i=1;i<m;++i){
		res += sol2(n-i, i);
		//cout << "sol2(" << (n-i) << "," << i << ")=" << sol2(n-i,i) << endl;
	}
	dp2[n][m]=res;
	return res;
}
int sol3(int n, int m){
	if(n==0) return 1;
	if(n<0 || m<=0) return 0;
	if(dp3[n][m]!=-1)
		return dp3[n][m];
	int res=0;
	for(int i=1;i<=m;i+=2){
		res += sol3(n-i, i);
	}
	dp3[n][m]=res;
	return res;
}

int main(){
	int n,k;
	memset(dp1,-1,sizeof(dp1));
	memset(dp2,-1,sizeof(dp2));
	memset(dp3,-1,sizeof(dp3));
	while(cin >> n >> k){
		cout << sol1(n,n+1,k) << endl;
		cout << sol2(n,n+1) << endl;
		cout << sol3(n,n+1) << endl;
	}
	return 0;
}
016:Charm Bracelet

总时间限制: 1000ms 内存限制: 65536kB

描述

Bessie has gone to the mall’s jewelry store and spies a charm bracelet. Of course, she’d like to fill it with the best charms possible from the N(1 ≤ N≤ 3,402) available charms. Each charm iin the supplied list has a weight Wi(1 ≤ Wi≤ 400), a ‘desirability’ factor Di(1 ≤ Di≤ 100), and can be used at most once. Bessie can only support a charm bracelet whose weight is no more than M(1 ≤ M≤ 12,880).

Given that weight limit as a constraint and a list of the charms with their weights and desirability rating, deduce the maximum possible sum of ratings.

输入

Line 1: Two space-separated integers: N and M
Lines 2…N+1: Line i+1 describes charm i with two space-separated integers: Wi and Di

输出

Line 1: A single integer that is the greatest sum of charm desirabilities that can be achieved given the weight constraints

样例输入
4 6
1 4
2 6
3 12
2 7
样例输出
23
来源

USACO 2007 December Silver

一个参考解答
#include<iostream>

using namespace std;

int main(){
	int n,m;
	cin >> n >> m;
	int * dp = new int[m+2];
	int w,d;
	cin >> w >> d;
	for(int i=0;i<w;++i){dp[i]=0;}
	for(int i=w;i<=m;++i){dp[i]=d;}
	while(--n){
		cin >> w >> d;
		for(int i=m;i>=w;--i){
			dp[i]=max(dp[i], dp[i-w]+d);
		}
	}
	cout << dp[m] << endl;
	delete[] dp;
	return 0;
}
017:分蛋糕

总时间限制: 1000ms 内存限制: 65536kB

描述

有一块矩形大蛋糕,长和宽分别是整数w 、h。现要将其切成m块小蛋糕,每个小蛋糕都必须是矩形、且长和宽均为整数。切蛋糕时,每次切一块蛋糕,将其分成两个矩形蛋糕。请计算:最后得到的m块小蛋糕中,最大的那块蛋糕的面积下限。

假设w= 4, h= 4, m= 4,则下面的切法可使得其中最大蛋糕块的面积最小。

在这里插入图片描述

假设w= 4, h= 4, m= 3,则下面的切法会使得其中最大蛋糕块的面积最小:

在这里插入图片描述

输入

共有多行,每行表示一个测试案例。每行是三个用空格分开的整数w, h, m ,其中1 ≤ w, h, m ≤ 20 , m ≤ wh. 当 w = h = m = 0 时不需要处理,表示输入结束。

输出

每个测试案例的结果占一行,输出一个整数,表示最大蛋糕块的面积下限。

样例输入
4 4 4
4 4 3
0 0 0
样例输出
4
6
一个参考解答
#include<iostream>
#include<cstring>

using namespace std;

const int mINF = 410;
int dp[21][21][21]={0};
int solve(int w, int h, int m){
	//cout << "solve-"<<w<<"-"<<h<<"-"<<m<<"=";
	if(dp[w][h][m]!=-1) {
		//cout << dp[w][h][m]<<endl;
		return dp[w][h][m];
	}
	int res;
	if(w*h<m) res = mINF;
	else if(w*h==m) res = 1;
	else if(m==0) res=mINF;
	else if(m==1) res=w*h;
	else if(m==2){
		if(w%2==0 || h%2==0) res=w*h/2;
		else if(w<h) res = w*(h/2+1);
		else res = h*(w/2+1);
	}
	else {
		int SV=mINF,SH=mINF;
		for(int i=1;i<w;++i){
			for(int j=1;j<m-1;++j){
				SV = min(SV, max(solve(i,h,j),solve(w-i,h,m-j)));
			}
		}
		for(int i=1;i<h;++i){
			for(int j=1;j<m-1;++j){
				SH = min(SH, max(solve(w,i,j),solve(w,h-i,m-j)));
			}
		}
		res = min(SV,SH);
	}
	//cout << res << endl;
	dp[w][h][m] = res;
	return res;
}

int main(){
	int w,h,m;
	memset(dp,-1,sizeof(dp));
	cin >> w >> h >> m;
	while(!(w==0 && h==0 && m==0)){
		// solve
		cout << solve(w,h,m) << endl;
		cin >> w >> h >> m;
	}
	return 0;
}
018:红与黑

总时间限制: 1000ms 内存限制: 65536kB

描述

有一间长方形的房子,地上铺了红色、黑色两种颜色的正方形瓷砖。你站在其中一块黑色的瓷砖上,只能向相邻的黑色瓷砖移动。请写一个程序,计算你总共能够到达多少块黑色的瓷砖。

输入

包括多个数据集合。每个数据集合的第一行是两个整数W和H,分别表示x方向和y方向瓷砖的数量。W和H都不超过20。在接下来的H行中,每行包括W个字符。每个字符表示一块瓷砖的颜色,规则如下
1)‘.’:黑色的瓷砖;
2)‘#’:红色的瓷砖;
3)‘@’:黑色的瓷砖,并且你站在这块瓷砖上。该字符在每个数据集合中唯一出现一次。
当在一行中读入的是两个零时,表示输入结束。

输出

对每个数据集合,分别输出一行,显示你从初始位置出发能到达的瓷砖数(记数时包括初始位置的瓷砖)。

样例输入
6 9 
....#. 
.....# 
...... 
...... 
...... 
...... 
...... 
#@...# 
.#..#. 
0 0
样例输出
45
来源

1979

一个参考解答
#include<iostream>
#include<cstring>
#include<queue>

using namespace std;

struct Node{
	int x,y;
	Node(int _x, int _y):x(_x),y(_y){}
	Node(){}
};

int main(){
	int w,h;
	char brick[22][22]={0};
	bool dd[22][22]={false};
	queue<Node> mq;
	int cnt=0;
	cin >> w >> h;
	while(!(w==0 && h==0)){
		memset(brick,0,sizeof(brick));
		memset(dd,0,sizeof(dd));
	cin.get();
	for(int i=0;i<h;++i){
		cin.getline(&brick[i+1][1],21);
		//cin.getline(brick[i],22);
	}
	int ix=0,iy=0;
	for(int i=1;i<=h;++i){
		if(ix!=0) break;
		for(int j=1;j<=w;++j){
			if(brick[i][j]=='@'){
				ix=j;iy=i;cnt=1;
				break;
			}
		}
	}
	//cout << "my:" << ix << " " << iy << endl;
	mq.push(Node(ix,iy));
	dd[iy][ix]=true;
	while(!mq.empty()){
		Node node=mq.front();
		mq.pop();
		if(brick[node.y-1][node.x] == '.' && !dd[node.y-1][node.x]){
			mq.push(Node(node.x,node.y-1));
			dd[node.y-1][node.x] = true;++cnt;
		}
		if(brick[node.y][node.x-1] == '.' && !dd[node.y][node.x-1]){
			mq.push(Node(node.x-1,node.y));
			dd[node.y][node.x-1] = true;++cnt;
		}
		if(brick[node.y+1][node.x] == '.' && !dd[node.y+1][node.x]){
			mq.push(Node(node.x,node.y+1));
			dd[node.y+1][node.x] = true;++cnt;
		}
		if(brick[node.y][node.x+1] == '.' && !dd[node.y][node.x+1]){
			mq.push(Node(node.x+1,node.y));
			dd[node.y][node.x+1] = true;++cnt;
		}
	}
	/*for(int i=0;i<22;++i){
		for(int j=0;j<22;++j){
			if(brick[i][j] != 0)
				printf("%c ",brick[i][j]);
			else 
				printf("0 ");
		}
		putchar('\n');
	}
	cout << "------" << endl;
	for(int i=0;i<22;++i){
		for(int j=0;j<22;++j){
			printf("%d ",dd[i][j]);
		}
		putchar('\n');
	}*/
	cout << cnt << endl;
	cin >> w >> h;
	}
	return 0;
}
019:A Knight’s Journey

总时间限制: 1000ms 内存限制: 65536kB

描述

Background
The knight is getting bored of seeing the same black and white squares again and again and has decided to make a journey
around the world. Whenever a knight moves, it is two squares in one direction and one square perpendicular to this. The world of a knight is the chessboard he is living on. Our knight lives on a chessboard that has a smaller area than a regular 8 * 8 board, but it is still rectangular. Can you help this adventurous knight to make travel plans?
在这里插入图片描述
Problem
Find a path such that the knight visits every square once. The knight can start and end on any square of the board.

输入

The input begins with a positive integer n in the first line. The following lines contain n test cases. Each test case consists of a single line with two positive integers p and q, such that 1 <= p * q <= 26. This represents a p * q chessboard, where p describes how many different square numbers 1, . . . , p exist, q describes how many different square letters exist. These are the first q letters of the Latin alphabet: A, . . .

输出

The output for every scenario begins with a line containing “Scenario #i:”, where i is the number of the scenario starting at 1. Then print a single line containing the lexicographically first path that visits all squares of the chessboard with knight moves followed by an empty line. The path should be given on a single line by concatenating the names of the visited squares. Each square name consists of a capital letter followed by a number.
If no such path exist, you should output impossible on a single line.

样例输入
3
1 1
2 3
4 3
样例输出
Scenario #1:
A1

Scenario #2:
impossible

Scenario #3:
A1B3C1A2B4C2A3B1C3A4B2C4
来源

TUD Programming Contest 2005, Darmstadt, Germany

一个参考解答

注意要按“lexicographically”顺序,
参考这里

#include<iostream>
#include<cstring>

using namespace std;

struct Node{
	int r,c;
	Node(int _r,int _c):r(_r),c(_c){}
	Node(){}
};

int n,p,q,squares;
int dq[] = {-2,-2,-1,-1,1,1,2,2};
int dp[] = {-1,1,-2,2,-2,2,-1,1};
bool bd[8][8]={false};
Node mq[65];
bool fl=false;

bool dfs(int r, int c, int t){
	bd[r][c]=true;
	mq[t]=Node(r,c);
	if(t==squares) return true;
	for(int i=0;i<8;++i){
		int nr=r+dp[i];
		int nc=c+dq[i];
		if(!bd[nr][nc] && nr>=0 && nr<p && nc>=0 && nc<q){
			if(dfs(nr,nc,t+1))
				return true;
		}
	}
	bd[r][c]=false;
	return false;
}

int main(){
	cin >> n;
	for(int i=1;i<=n;++i){
		cin >> p >> q;
		squares=p*q-1;
		cout << "Scenario #" << i << ":" << endl;
		if(p==1 && q==1){
			cout << "A1\n" << endl;
			continue;
		}
		if(p>8||q>8||p<=2||q<=2){
			cout << "impossible\n" << endl;
			continue;
		}
		memset(bd,false,sizeof(bd));
		fl=false;
		for(int y=0;y<p;++y){
			for(int x=0;x<q;++x){
				mq[0]=Node(y,x);
				if(dfs(y,x,0)){
					fl=true;
					for(int k=0;k<p*q;++k){
						printf("%c%d",'A'+mq[k].c,mq[k].r+1);
					}
					cout << endl;
					break;
				}
			}
			if(fl) break;
		}
		if(!fl) {
			cout << "impossible" << endl;
		}
		cout << endl;
	}
	
	
	return 0;
}
020:棋盘问题

总时间限制: 1000ms 内存限制: 65536kB

描述

在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别。要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子的所有可行的摆放方案C。

输入

输入含有多组测试数据。
每组数据的第一行是两个正整数,n k,用一个空格隔开,表示了将在一个n*n的矩阵内描述棋盘,以及摆放棋子的数目。 n <= 8 , k <= n
当为-1 -1时表示输入结束。
随后的n行描述了棋盘的形状:每行有n个字符,其中 # 表示棋盘区域, . 表示空白区域(数据保证不出现多余的空白行或者空白列)。

输出

对于每一组数据,给出一行输出,输出摆放的方案数目C (数据保证C<2^31)。

样例输入
2 1
#.
.#
4 4
...#
..#.
.#..
#...
-1 -1
样例输出
2
1
来源

蔡错@pku

一个参考解答
#include<iostream>
#include<cstring>

using namespace std;

int n,k;
int cnt,fnum;
struct Flag{
	int r,c;
	Flag(int _r,int _c):r(_r),c(_c){};
	Flag(){};
};
Flag flags[65];
bool board[10][10]={false};
bool raws[10]={false};
bool cols[10]={false};

void solve(int ind, int k){
	//cout << "solve " << ind << " " << k << endl;
	if(k==0){++cnt;return;}
	if(ind>=fnum) return;
	if(fnum-ind<k) return;
	if(fnum>ind+1) solve(ind+1,k);
	Flag fl=flags[ind];
	if(raws[fl.r] || cols[fl.c]) return;
	else {
		raws[fl.r] = true;
		cols[fl.c] = true;
		solve(ind+1,k-1);
		raws[fl.r] = false;
		cols[fl.c] = false;
	}
}

int main(){
	char tp[10];
	cin >> n >> k;
	cin.get();
	while(!(n==-1 && k==-1)){
		cnt=0;fnum=0;
		memset(raws,false,sizeof(raws));
		memset(cols,false,sizeof(cols));
		for(int i=0;i<n;++i){
			cin.getline(tp,10);
			for(int j=0;j<n;++j){
				if(tp[j]=='#'){
					board[i][j]=true;
					flags[fnum++]=Flag(i,j);
				}
				else board[i][j]=false;
			}
		}
		solve(0,k);
		cout << cnt << endl;
		cin >> n >> k;
		cin.get();
	}
	return 0;
}
021:鸣人和佐助

总时间限制: 1000ms 内存限制: 65536kB

描述

佐助被大蛇丸诱骗走了,鸣人在多少时间内能追上他呢?
已知一张地图(以二维矩阵的形式表示)以及佐助和鸣人的位置。地图上的每个位置都可以走到,只不过有些位置上有大蛇丸的手下,需要先打败大蛇丸的手下才能到这些位置。鸣人有一定数量的查克拉,每一个单位的查克拉可以打败一个大蛇丸的手下。假设鸣人可以往上下左右四个方向移动,每移动一个距离需要花费1个单位时间,打败大蛇丸的手下不需要时间。如果鸣人查克拉消耗完了,则只可以走到没有大蛇丸手下的位置,不可以再移动到有大蛇丸手下的位置。佐助在此期间不移动,大蛇丸的手下也不移动。请问,鸣人要追上佐助最少需要花费多少时间?

输入

输入的第一行包含三个整数:M,N,T。代表M行N列的地图和鸣人初始的查克拉数量T。0 < M,N < 200,0 ≤ T < 10
后面是M行N列的地图,其中@代表鸣人,+代表佐助。*代表通路,#代表大蛇丸的手下。

输出

输出包含一个整数R,代表鸣人追上佐助最少需要花费的时间。如果鸣人无法追上佐助,则输出-1。

样例输入
样例输入1
4 4 1
#@##
**##
###+
****

样例输入2
4 4 2
#@##
**##
###+
****
样例输出
样例输出1
6

样例输出2
4
一个参考解答

这个写的有点重复

#include<iostream>

using namespace std;

struct Node{
	int r,c,ti,tl;
	Node(int _r,int _c,int _ti,int _tl):r(_r),c(_c),ti(_ti),tl(_tl){};
	Node(){};
};

int main(){
	int m,n,t;
	char bd[202][202]={0};
	bool ntl[202][202][10]={false};
	//bool dd[202][202]={false};
	Node mq[100000];
	int qHead=0,qTail=1;
	cin >> m >> n >> t;
	cin.get();
	for(int i=0;i<m;++i){
		cin.getline(bd[i],202);
		for(int j=0;j<n;++j){
			if(bd[i][j]=='@'){
				//dd[i][j]=true;
				ntl[i][j][t]=true;
				mq[0]=Node(i,j,0,t);
			}
		}
	}
	//for(int i=0;i<m;++i){
	//	puts(bd[i]);
	//}
	bool isFind = false;
	while(qHead!=qTail){
		Node node=mq[qHead];
		if(node.r>0){// && !dd[node.r-1][node.c]){
			switch(bd[node.r-1][node.c]){
				case '@':
					break;
				case '+':
					cout << (node.ti+1) << endl;
					isFind = true;break;
				case '*':
					if(!ntl[node.r-1][node.c][node.tl]){
						mq[qTail++]=Node(node.r-1,node.c,node.ti+1,node.tl);
						ntl[node.r-1][node.c][node.tl]=true;
					}
					//dd[node.r-1][node.c]=true;
					break;
				case '#':
					if(node.tl>0 && !ntl[node.r-1][node.c][node.tl-1]){
						mq[qTail++]=Node(node.r-1,node.c,node.ti+1,node.tl-1);
						ntl[node.r-1][node.c][node.tl-1]=true;
						//dd[node.r-1][node.c]=true;
					}
					break;
			}
			if(isFind) break;
		}
		if(node.r+1<m){// && !dd[node.r+1][node.c]){
			switch(bd[node.r+1][node.c]){
				case '@':
					break;
				case '+':
					cout << (node.ti+1) << endl;
					isFind = true;break;
				case '*':
					if(!ntl[node.r+1][node.c][node.tl]){
						mq[qTail++]=Node(node.r+1,node.c,node.ti+1,node.tl);
						ntl[node.r+1][node.c][node.tl]=true;
					}
					//dd[node.r+1][node.c]=true;
					break;
				case '#':
					if(node.tl>0 && !ntl[node.r+1][node.c][node.tl-1]){
						mq[qTail++]=Node(node.r+1,node.c,node.ti+1,node.tl-1);
						ntl[node.r+1][node.c][node.tl-1]=true;
						//dd[node.r+1][node.c]=true;
					}
					break;
			}
			if(isFind) break;
		}
		if(node.c>0){// && !dd[node.r][node.c-1]){
			switch(bd[node.r][node.c-1]){
				case '@':
					break;
				case '+':
					cout << (node.ti+1) << endl;
					isFind = true;break;
				case '*':
					if(!ntl[node.r][node.c-1][node.tl]){
						mq[qTail++]=Node(node.r,node.c-1,node.ti+1,node.tl);
						ntl[node.r][node.c-1][node.tl]=true;
					}
					//dd[node.r][node.c-1]=true;
					break;
				case '#':
					if(node.tl>0 && !ntl[node.r][node.c-1][node.tl-1]){
						mq[qTail++]=Node(node.r,node.c-1,node.ti+1,node.tl-1);
						ntl[node.r][node.c-1][node.tl-1]=true ;
						//dd[node.r][node.c-1]=true;
					}
					break;
			}
			if(isFind) break;
		}
		if(node.c+1<n){// && !dd[node.r][node.c+1]){
			switch(bd[node.r][node.c+1]){
				case '@':
					break;
				case '+':
					cout << (node.ti+1) << endl;
					isFind = true;break;
				case '*':
					if(!ntl[node.r][node.c+1][node.tl]){
						mq[qTail++]=Node(node.r,node.c+1,node.ti+1,node.tl);
						ntl[node.r][node.c+1][node.tl]=true;
					}
					//dd[node.r][node.c+1]=true;
					break;
				case '#':
					if(node.tl>0 && !ntl[node.r][node.c+1][node.tl-1]){
						mq[qTail++]=Node(node.r,node.c+1,node.ti+1,node.tl-1);
						ntl[node.r][node.c+1][node.tl-1]=true;
						//dd[node.r][node.c+1]=true;
					}
					break;
			}
			if(isFind) break;
		}
		++qHead;
	}
	if(!isFind) cout << "-1" << endl;
	/*for(int i=0;i<qTail;++i){
		Node nd=mq[i];
		cout << nd.r << " " << nd.c << " " << nd.ti << " " << nd.tl << endl;
	}*/
	return 0;
}
022:迷宫问题

总时间限制: 1000ms 内存限制: 65536kB

描述

定义一个二维数组:


int maze[5][5] = {

0, 1, 0, 0, 0,

0, 1, 0, 1, 0,

0, 0, 0, 0, 0,

0, 1, 1, 1, 0,

0, 0, 0, 1, 0,

};

它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线。

输入

一个5 × 5的二维数组,表示一个迷宫。数据保证有唯一解。

输出

左上角到右下角的最短路径,格式如样例所示。

样例输入
0 1 0 0 0
0 1 0 1 0
0 0 0 0 0
0 1 1 1 0
0 0 0 1 0
样例输出
(0, 0)
(1, 0)
(2, 0)
(2, 1)
(2, 2)
(2, 3)
(2, 4)
(3, 4)
(4, 4)
一个参考解答
#include<iostream>
//#include<sstream>
#include<string>

using namespace std;

struct Node{
	int r,c;
	int father;
	Node(int _r,int _c,int f):r(_r),c(_c),father(f){};
	Node(){};
};

int main(){
	int maze[5][5]={0};
	bool dd[5][5]={false};
	Node mq[50];
	int qHead=0,qTail=1;
	mq[0]=Node(0,0,-1);
	dd[0][0]=true;
	cin >> maze[0][0] >> maze[0][1] >> maze[0][2] >> maze[0][3] >> maze[0][4] ;
	cin >> maze[1][0] >> maze[1][1] >> maze[1][2] >> maze[1][3] >> maze[1][4] ;
	cin >> maze[2][0] >> maze[2][1] >> maze[2][2] >> maze[2][3] >> maze[2][4] ;
	cin >> maze[3][0] >> maze[3][1] >> maze[3][2] >> maze[3][3] >> maze[3][4] ;
	cin >> maze[4][0] >> maze[4][1] >> maze[4][2] >> maze[4][3] >> maze[4][4] ;
	while(qHead!=qTail){
		Node node=mq[qHead];
		int nr=node.r,nc=node.c;
		if(nr>0 && !dd[nr-1][nc] && maze[nr-1][nc]==0){
			dd[nr-1][nc] = true;
			mq[qTail++] = Node(nr-1, nc, qHead);
		}
		if(nc>0 && !dd[nr][nc-1] && maze[nr][nc-1]==0){
			dd[nr][nc-1]=true;
			mq[qTail++] = Node(nr, nc-1, qHead);
		}
		if(nr<4 && !dd[nr+1][nc] && maze[nr+1][nc]==0){
			dd[nr+1][nc]=true;
			mq[qTail++] = Node(nr+1, nc, qHead);
			if(nr==3 && nc==4){qHead=qTail;break;}
		}
		if(nc<4 && !dd[nr][nc+1] && maze[nr][nc+1]==0){
			dd[nr][nc+1] = true;
			mq[qTail++] = Node(nr, nc+1, qHead);
			if(nr==4 && nc==3){qHead=qTail;break;}
		}
		++qHead;
	}
	Node nd=mq[--qHead];
	string s;
	//ostringstream buf;
	char tp[10];
	//if(!(nd.r==4 && nd.c==4)){
	//	cout << "imposible" << endl;
	//} else {
		while(nd.father != -1){
			sprintf(tp, "\n(%d, %d)",nd.r,nd.c);
			s = string(tp) + s;
			nd = mq[nd.father];
			//cout << "(" << nd.r << "," << nd.c << ")" << endl;
		}
		cout << "(0, 0)" << s << endl;
	//}
	return 0;
}
023:Pots

总时间限制: 1000ms 内存限制: 65536kB

描述

You are given two pots, having the volume of A and B liters respectively. The following operations can be performed:

FILL(i)        fill the pot i (1 ≤ i ≤ 2) from the tap;

DROP(i)      empty the pot i to the drain;

POUR(i,j)    pour from pot i to pot j; after this operation either the pot j is full (and there may be some water left in the pot i), or the pot i is empty (and all its contents have been moved to the pot j).

Write a program to find the shortest possible sequence of these operations that will yield exactly C liters of water in one of the pots.

输入

On the first and only line are the numbers A, B, and C. These are all integers in the range from 1 to 100 and C≤max(A,B).

输出

The first line of the output must contain the length of the sequence of operations K. The following K lines must each describe one operation. If there are several sequences of minimal length, output any one of them. If the desired result can’t be achieved, the first and only line of the file must contain the word ‘impossible’.

样例输入
3 5 4
样例输出
6
FILL(2)
POUR(2,1)
DROP(1)
POUR(2,1)
FILL(2)
POUR(2,1)
一个参考解答
#include<iostream>
#include<string>

using namespace std;

/*
0 ""
1 FILL(1)
2 FILL(2)
3 DROP(1)
4 DROP(2)
5 POUR(2,1)
6 POUR(1,2)
*/


struct Node{
	int na,nb;
	int father;
	int move;
	Node(int a,int b,int f,int m):na(a),nb(b),father(f),move(m){};
	Node(){};
};

int main(){
	Node mq[100000];
	bool dd[101][101]={false};
	mq[0]=Node(0,0,-1,0);
	int qHead=0,qTail=1;
	int maxA,maxB,tag;
	cin >> maxA >> maxB >> tag;
	if(maxA==tag){
		cout << 1 << endl << "FILL(1)" <<endl;
	} else if(maxB==tag){
		cout << 1 << endl << "FILL(2)" <<endl;
	} else {
	while(qHead!=qTail){
		Node nd=mq[qHead];
		int nextA,nextB;
		if(nd.na!=maxA){
			if(!dd[maxA][nd.nb]){
				dd[maxA][nd.nb]=true;
				mq[qTail++]=Node(maxA,nd.nb,qHead,1);
			}
			if(nd.nb!=0){
				nextB = nd.nb-maxA+nd.na;
				if(nextB>=0){
					nextA = maxA;
				} else {
					nextA=nd.nb+nd.na;
					nextB=0;
				}
				if(!dd[nextA][nextB]){
					dd[nextA][nextB]=true;
					mq[qTail++]=Node(nextA,nextB,qHead,5);
					if(nextA==tag || nextB==tag){
						qHead=qTail;break;
					}
				}
			}
		}
		if(nd.na!=0 && !dd[0][nd.nb]){
			dd[0][nd.nb]=true;
			mq[qTail++]=Node(0,nd.nb,qHead,3);
		}
		if(nd.nb!=maxB){
			if(!dd[nd.na][maxB]){
				dd[nd.na][maxB]=true;
				mq[qTail++]=Node(nd.na,maxB,qHead,2);
			}
			if(nd.na!=0){
				nextA = nd.nb-maxB+nd.na;
				if(nextA>=0){
					nextB = maxB;
				} else {
					nextB=nd.nb+nd.na;
					nextA=0;
				}
				if(!dd[nextA][nextB]){
					dd[nextA][nextB]=true;
					mq[qTail++]=Node(nextA,nextB,qHead,6);
					if(nextA==tag || nextB==tag){
						qHead=qTail;break;
					}
				}
			}
		}
		if(nd.nb!=0 && !dd[nd.na][0]){
			dd[nd.na][0]=true;
			mq[qTail++]=Node(nd.na,0,qHead,4);
		}
		++qHead;
	}
	Node pt=mq[--qHead];
	if(!(pt.na==tag || pt.nb==tag)){
		cout << "impossible" << endl;
	} else {
		int cnt=0;
		string s;
		do{
			switch(pt.move){
				case 0:
					break;
				case 1:
					s = "FILL(1)\n" + s;break;
				case 2:
					s = "FILL(2)\n" + s;break;
				case 3:
					s = "DROP(1)\n" + s;break;
				case 4:
					s = "DROP(2)\n" + s;break;
				case 5:
					s = "POUR(2,1)\n" + s;break;
				case 6:
					s = "POUR(1,2)\n" + s;break;
			}
			++cnt;
			pt = mq[pt.father];
		}while(pt.father!=-1);
		cout << cnt << endl;
		cout << s;
	}
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

今夕何夕2112

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值