The Process Of BFS.

本文通过一个简单的示例展示了宽度优先搜索(BFS)算法的工作原理。使用C++编程语言实现了一个二维地图上的节点遍历过程,并实时展示算法执行的每一步。用户可以输入起始坐标,程序将从该坐标开始进行宽度优先遍历。
//This codes will show u how BFS algorithm works.<pre name="code" class="cpp">#include<iostream>
#include<string>
#include<list>
#include<windows.h>
using namespace std;
char map[50][50]={
						"                    ",
						"                    ",
						"                    ",
						"                    ",
						"                    ",
						"                    ",
						"                    ",
						"                    ",
						"                    ",
						"                    ",
	};
class coordinate
{
public:
	int x,y;
	coordinate(int xx,int yy):x(xx),y(yy){}
};
void show()
{
	int i,j;
	for(i=0;i<10;i++)
	{
		for(j=0;j<20;j++)
			cout<<map[i][j];
		cout<<endl;
	}
	Sleep(100);//flash time if u wanne see faster ,erase it.
	system("cls");
}
int main()
{

	list<coordinate>queue;
	int visited[100][100]={0},start_x,start_y,next_x,next_y,row,col,dir,direction[4][2]={{-1,0},{0,-1},{1,0},{0,1}};
	cout<<"Input the start coordinate which x range from 0 to 10 and y range 0 to 20(eg intput: 5 2)"<<endl;
	cin>>start_x>>start_y;
	row=10;
	col=20;
	visited[start_x][start_y]=1;
	map[start_x][start_y]='*';
	queue.push_back(coordinate(start_x,start_y));
	while(!queue.empty())
	{
		for(dir=0;dir<=3;dir++)
		{
			next_x=queue.front().x+direction[dir][0];
			next_y=queue.front().y+direction[dir][1];
			if(next_x<0||next_y<0||next_x>=row||next_y>=col)
				continue;
			else if(visited[next_x][next_y]==0)
			{
				show();
				map[next_x][next_y]='*';
				visited[next_x][next_y]=1;
				queue.push_back(coordinate(next_x,next_y));
				show();
			}
		}
		queue.pop_front();
	}
	return 0;
}



                
TCUTTER - Tin Cutter In a Tin Cutting factory there is a machine for cutting parts from tin plates. It has an extraordinarily sharp knife able to make horizontal or vertical segment cuts in the tin plates. Each cutting process consists of a sequence of such cuts. Each segment cut is given by its endpoints that are always located inside the tin plate. During the cutting process some parts of tin plate can fall out and so some holes in the plate can emerge. Factory management needs to predict the number of holes in the plate at the end of the given sequence of cuts. Write a program that answers this question. Single segment cuts are not considered to be holes. Here there are examples of some situations that can arise after cutting: two holes two holes one hole one hole Input The input file consists of blocks of lines. Each block except the last describes one cutting process. In the first line of the block there is a number N <= 100. indicating the number of segment cuts in the cutting process. These cuts are defined by the following N lines. The line defining one segment cut has the form X1 Y1 X2 Y2 where X1 Y1 and X2 Y2 are the co-ordinates of the end points of the segment cut. They are separated by one space. The co-ordinates are integers and always define horizontal or vertical segment (i.e. segment parallel with x or y axis). The last block consists of just one line containing 0. Output The output file contains the lines corresponding to the blocks in the input file. Each such line contains the number of holes that remain in the tin plate after the execution of the corresponding cuts. There is no line in the output file corresponding to the last "null" block of the input file. Example Input: 4 0 1 1 1 1 1 1 0 1 0 0 0 0 0 0 1 2 0 1 2 1 1 2 1 0 0 Output: 1 0
07-04
A player wants to press the direction buttons as little as possible so that both frogs arrive at the destination to finish the game. Find the minimum number of times the player pressed the buttons to finish the game. If the frog or green frog cannot arrive at the destination, output -1. [Figure] For example, let's look at the case where a grid with six rows and six columns is given. The frog is located at (6, 1), the green frog is located at (1, 6), and the destination is (2, 3) as shown in the [Figure]. Walls are marked with #. First, the frog arrives at the destination first by moving eight times as shown on the left side of the [Figure], and the green frog moves to the opposite direction at the same time and is located at (6, 4). Next, as shown on the right side of the [Figure], when the green frog arrives at the destination by moving it five times, you pressed the buttons 13 times for both frogs to arrive at the destination. This is the minimum number of times you press the buttons. [Constraints] 1. N and M are integers from 2 to 40. 2. R1, R2, and R3 are integers from 1 to N. 3. C1, C2, and R3 are integers from 1 to M. 4. Both frogs cannot be at the same location while they are moving. 5. Walls are not the start and destination of both frogs, and the start positions of the two frogs are not their destinations. [Input] First, the number T of test cases is given, followed by T test cases. On the first line of each test case, N and M are given, separated by spaces. On the next line, R1, C1, R2, C2, R3, and C3 are given in the order, separated by spaces. Over the next N lines, a string of length M is given..” represents a position in the grid to which both frogs can move, and “#” represents a wall. [Output] Output one line per test case. For each test case, output “#x” (where x is the number of the test case, starting from 1), leave a space, and then output the answer of the relevant test case. [Example of Input and Output] (Input) 1 6 6 6 1 1 6 2 3 ....#. .#..#. .#..#. .#..#. .#.##. .#....
最新发布
09-16
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值