暑假训练DAY10测试(贪心+BFS)(水题部分)

A. Pythagorean Theorem II

CodeForces - 304A 

http://codeforces.com/problemset/problem/304/A

time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

In mathematics, the Pythagorean theorem — is a relation in Euclidean geometry among the three sides of a right-angled triangle. In terms of areas, it states:

In any right-angled triangle, the area of the square whose side is the hypotenuse (the side opposite the right angle) is equal to the sum of the areas of the squares whose sides are the two legs (the two sides that meet at a right angle).

The theorem can be written as an equation relating the lengths of the sides ab and c, often called the Pythagorean equation:

a2 + b2 = c2

where c represents the length of the hypotenuse, and a and b represent the lengths of the other two sides.

Given n, your task is to count how many right-angled triangles with side-lengths ab and c that satisfied an inequality 1 ≤ a ≤ b ≤ c ≤ n.

Input

The only line contains one integer n (1 ≤ n ≤ 104) as we mentioned above.

Output

Print a single integer — the answer to the problem.

Examples

input

5

output

1

input

74

output

35

水题,直接暴力枚举就好了

#include<algorithm>
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<vector>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>

using namespace std;
const double eps=1e-8;
int dcmp(double x)
{
	if(fabs(x)<=eps) return 0;
	if(x>0) return 1;
	return -1;
}
int main()
{
	int n;
	while(cin>>n)
	{
		int ans=0;
		int i,j;
		for(i=5;i<=n;i++)
		{
			for(j=1;j<sqrt(i*i/2);j++)
			{
				double k=sqrt((i*i-j*j));
				//cout<<k<<endl;
				if(dcmp(k-(int)k)==0)
				{
					ans++;
				}
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}

C - 迷宫问题

 POJ - 3984 

定义一个二维数组: 

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表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线。

Input

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

Output

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

Sample Input

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

Sample Output

(0, 0)
(1, 0)
(2, 0)
(2, 1)
(2, 2)
(2, 3)
(2, 4)
(3, 4)
(4, 4)

裸BFS,顺便记录一下路径就好了

#include<algorithm>
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<vector>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>

using namespace std;
struct node{
	int x,y;
};
node befo[10][10];
int m[10][10];
int flag[10][10];
int mve[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
queue<node> q;
node bfs()
{
	node fir;
	fir.x=0;
	fir.y=0;
	q.push(fir);
	flag[0][0]=1;
	while(!q.empty())
	{
		node u=q.front();
		q.pop();
		if(u.x==4&&u.y==4) return u;
		for(int i=0;i<4;i++)
		{
			node zz;
			zz.x=u.x+mve[i][0];
			zz.y=u.y+mve[i][1];
			if(zz.x>=0&&zz.x<5&&zz.y>=0&&zz.x<5&&!flag[zz.x][zz.y]&&m[zz.x][zz.y]==0)
			{
				flag[zz.x][zz.y]=1;
				q.push(zz);
				befo[zz.x][zz.y]=u;
			}
		}
	}
}
int main()
{
	int i,j;
	memset(flag,0,sizeof(flag));
	for(i=0;i<5;i++)
	{
		for(j=0;j<5;j++)
		{
			scanf("%d",&m[i][j]);
		}
	}
	node last=bfs();
	node roar[25];
	int cnt=0;
	for(;last.x!=0||last.y!=0;last=befo[last.x][last.y])
	{
		roar[cnt++]=last;
	}
	printf("(%d, %d)\n",0,0);
	for(i=cnt-1;i>=0;i--)
	{
		printf("(%d, %d)\n",roar[i].x,roar[i].y);
	}
	return 0;
}

E - Key Task

 HDU - 1885 

The Czech Technical University is rather old — you already know that it celebrates 300 years of its existence in 2007. Some of the university buildings are old as well. And the navigation in old buildings can sometimes be a little bit tricky, because of strange long corridors that fork and join at absolutely unexpected places. 

The result is that some first-graders have often di?culties finding the right way to their classes. Therefore, the Student Union has developed a computer game to help the students to practice their orientation skills. The goal of the game is to find the way out of a labyrinth. Your task is to write a verification software that solves this game. 

The labyrinth is a 2-dimensional grid of squares, each square is either free or filled with a wall. Some of the free squares may contain doors or keys. There are four di?erent types of keys and doors: blue, yellow, red, and green. Each key can open only doors of the same color. 

You can move between adjacent free squares vertically or horizontally, diagonal movement is not allowed. You may not go across walls and you cannot leave the labyrinth area. If a square contains a door, you may go there only if you have stepped on a square with an appropriate key before.

Input

The input consists of several maps. Each map begins with a line containing two integer numbers R and C (1 ≤ R, C ≤ 100) specifying the map size. Then there are R lines each containing C characters. Each character is one of the following: 



Note that it is allowed to have 

  • more than one exit,
  • no exit at all,
  • more doors and/or keys of the same color, and
  • keys without corresponding doors and vice versa.


     
  • You may assume that the marker of your position (“*”) will appear exactly once in every map. 

    There is one blank line after each map. The input is terminated by two zeros in place of the map size.

Output

For each map, print one line containing the sentence “Escape possible in S steps.”, where S is the smallest possible number of step to reach any of the exits. If no exit can be reached, output the string “The poor student is trapped!” instead. 

One step is defined as a movement between two adjacent cells. Grabbing a key or unlocking a door does not count as a step.

Sample Input

1 10
*........X

1 3
*#X

3 20
####################
#XY.gBr.*.Rb.G.GG.y#
####################

0 0

Sample Output

Escape possible in 9 steps.
The poor student is trapped!
Escape possible in 45 steps.

状态压缩+BFS之前的博客写过一个几乎一样的题目了,这里就不解释了。有需要可以去到这篇博客看看

https://blog.csdn.net/baiyifeifei/article/details/81295624

#include<algorithm>
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<vector>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>

using namespace std;
struct node{
	int x;
	int y;
	int key;
	int step;
};
map<char,int> s;
char m[105][105];
int mve[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
int flag[105][105][100];
int  r,c;
queue<node> q;
int check(node k)
{
	if(k.x>=1&&k.x<=r&&k.y>=1&&k.y<=c&&m[k.x][k.y]!='#'&&!flag[k.x][k.y][k.key]) return 1;
	return 0;
}
int bfs(int x0,int y0)
{
	while(!q.empty()) q.pop();
	node u;
	u.x=x0,u.y=y0;
	u.key=0;u.step=0;
	flag[x0][y0][0]=1;
	q.push(u);
	while(!q.empty())
	{
		node fir=q.front();
		q.pop();
		//cout<<fir.x<<' '<<fir.y<<' '<<fir.step<<endl;
		if(m[fir.x][fir.y]=='X')
		{
			return fir.step;
		}
		for(int i=0;i<4;i++)
		{
			node nxt;
			nxt.x=fir.x+mve[i][0];
			nxt.y=fir.y+mve[i][1];
			nxt.key=fir.key;
			nxt.step=fir.step+1;
			if(check(nxt))
			{
				if(m[nxt.x][nxt.y]=='B'||m[nxt.x][nxt.y]=='Y'||m[nxt.x][nxt.y]=='R'||m[nxt.x][nxt.y]=='G')
				{
					if((nxt.key&(1<<s[m[nxt.x][nxt.y]]))!=(1<<s[m[nxt.x][nxt.y]])) continue; 
					//cout<<nxt.x<<' '<<nxt.y<<' '<<nxt.key<<' '<<nxt.step<<endl;
				}
				if(m[nxt.x][nxt.y]=='b'||m[nxt.x][nxt.y]=='y'||m[nxt.x][nxt.y]=='r'||m[nxt.x][nxt.y]=='g')
				{
					nxt.key=(nxt.key|(1<<s[m[nxt.x][nxt.y]]));
				}
				//nxt.step=fir.step+1;
				flag[nxt.x][nxt.y][nxt.key]=1;
				q.push(nxt);
			}
		}
	}
	return -1;
}
int main()
{
	s['b']=0,s['y']=1,s['r']=2,s['g']=3;
	s['B']=0,s['Y']=1,s['R']=2,s['G']=3;
	while(~scanf("%d%d",&r,&c)&&r)
	{
		memset(flag,0,sizeof(flag));
		memset(m,0,sizeof(m));
		int i,j;
		int sx=-1,sy=-1;
		for(i=1;i<=r;i++)
		{
			scanf("%s",m[i]+1);
			/*for(j=1;sx!=-1&&j<=c;j++)
			{
				if(m[i][j]=='*')
				{
					sx=i;
					sy=j;
				}
			}*/
		}
		int sign=0;
		for(i=1;i<=r;i++)
		{
			for(j=1;j<=c;j++)
			{
				if(m[i][j]=='*')
				{
					sx=i;
					sy=j;
					sign=1;
					break;
				}
			}
			if(sign) break;
		}
		//cout<<sx<<sy<<endl;
		int ans=bfs(sx,sy);
		if(ans==-1) printf("The poor student is trapped!\n");
		else printf("Escape possible in %d steps.\n",ans);
	}
	return 0;
}

这就是这次的水题部分了,还有一个比较难的题目我额外抽出来写了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值