首页 > ACM题库 > HDU-杭电 > HDU 1373 Channel Allocation-BFS-[解题报告] C++
2013
12-09

HDU 1373 Channel Allocation-BFS-[解题报告] C++

Channel Allocation

问题描述 :

When a radio station is broadcasting over a very large area, repeaters are used to retransmit the signal so that every receiver has a strong signal. However, the channels used by each repeater must be carefully chosen so that nearby repeaters do not interfere with one another. This condition is satisfied if adjacent repeaters use different channels.

Since the radio frequency spectrum is a precious resource, the number of channels required by a given network of repeaters should be minimised. You have to write a program that reads in a description of a repeater network and determines the minimum number of channels required.

输入:

The input consists of a number of maps of repeater networks. Each map begins with a line containing the number of repeaters. This is between 1 and 26, and the repeaters are referred to by consecutive upper-case letters of the alphabet starting with A. For example, ten repeaters would have the names A,B,C,…,I and J. A network with zero repeaters indicates the end of input.

Following the number of repeaters is a list of adjacency relationships. Each line has the form:

A:BCDH

which indicates that the repeaters B, C, D and H are adjacent to the repeater A. The first line describes those adjacent to repeater A, the second those adjacent to B, and so on for all of the repeaters. If a repeater is not adjacent to any other, its line has the form

A:

The repeaters are listed in alphabetical order.

Note that the adjacency is a symmetric relationship; if A is adjacent to B, then B is necessarily adjacent to A. Also, since the repeaters lie in a plane, the graph formed by connecting adjacent repeaters does not have any line segments that cross.

输出:

For each map (except the final one with no repeaters), print a line containing the minumum number of channels needed so that no adjacent channels interfere. The sample output shows the format of this line. Take care that channels is in the singular form when only one channel is required.

样例输入:

2
A:
B:
4
A:BC
B:ACD
C:ABD
D:BC
4
A:BCD
B:ACD
C:ABD
D:ABC
0

样例输出:

1 channel needed.
3 channels needed.
4 channels needed.

题目链接:点击打开链接

#include<iostream>
#include<queue>
#include<string>
using namespace std;
struct point
{
	int x,y,step;
};
int f[10][10];
int sx,sy,ex,ey;
int dir[][2]={1,2,2,1,2,-1,1,-2,-1,-2,-2,-1,-2,1,-1,2};
bool isval(int x,int y)
{
	if(x<=0||y<=0||x>8||y>8)return false;
	return true;
}
int bfs()
{
	queue<point>Q;
	point p;
	p.x=sx;p.y=sy;
	p.step=0;
	memset(f,0,sizeof(f));
	Q.push(p);
	while(!Q.empty())
	{
		point temp=Q.front();
		Q.pop();
		for(int i=0;i<8;i++)
		{
			point next;
			next.x=temp.x+dir[i][0];
			next.y=temp.y+dir[i][1];
			if(isval(next.x,next.y)&&f[next.x][next.y]==0)
			{
				next.step=temp.step+1;
				if(next.x==ex&&next.y==ey)return next.step;
				f[temp.x][temp.y]=1;
				Q.push(next);
			}
		}
	}
	return 0;
}
int main()
{
	char s[3],e[3];
	while(scanf("%s%s",s,e)!=EOF)
	{
		sx=s[0]-'a'+1;
		sy=s[1]-'0';
		ex=e[0]-'a'+1;
		ey=e[1]-'0';
		if(sx==ex&&sy==ey)
			printf("To get from %c%c to %c%c takes %d knight moves.\n",s[0],s[1],e[0],e[1],0);
		else printf("To get from %c%c to %c%c takes %d knight moves.\n",s[0],s[1],e[0],e[1],bfs());
	}
	return 0;
}

解题报告转自:http://blog.csdn.net/ssslpk/article/details/7782830


,
  1. 题本身没错,但是HDOJ放题目的时候,前面有个题目解释了什么是XXX定律。
    这里直接放了这个题目,肯定没几个人明白是干啥