A:Knight Moves(POJ1915)
Description
Background
Mr Somurolov, fabulous chess-gamer indeed, asserts that no one else but him can move knights from one position to another so fast. Can you beat him?
The Problem
Your task is to write a program to calculate the minimum number of moves needed for a knight to reach one point from another, so that you have the chance to be faster than Somurolov.
For people not familiar with chess, the possible knight moves are shown in Figure 1.
Input
The input begins with the number n of scenarios on a single line by itself.
Next follow n scenarios. Each scenario consists of three lines containing integer numbers. The first line specifies the length l of a side of the chess board (4 <= l <= 300). The entire board has size l * l. The second and third line contain pair of integers {0, …, l-1}*{0, …, l-1} specifying the starting and ending position of the knight on the board. The integers are separated by a single blank. You can assume that the positions are valid positions on the chess board of that scenario.
Output
For each scenario of the input you have to calculate the minimal amount of knight moves which are necessary to move from the starting point to the ending point. If starting point and ending point are equal,distance is zero. The distance must be written on a single line.
Sample Input
3
8
0 0
7 0
100
0 0
30 50
10
1 1
1 1
Sample Output
5
28
0
我的解答
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;
int pos[305][305];
int sx,sy,ex,ey,l;
int T;
int Move[8][2]={-2,1,-2,-1,-1,2,-1,-2,1,2,1,-2,2,1,2,-1};
struct Node{
int x,y,step;
};
int check(Node x){
if (x.x<0||x.x>=l||x.y<0||x.y>=l) {
return 1;
}else{
return pos[x.x][x.y];
}
}
void dfs(){
Node SN,next,temp;
SN.x = sx;SN.y = sy;SN.step = 0;
queue<Node>q;
pos[sx][sy] = 1;
q.push(SN);
while(!q.empty()){
temp = q.front();
q.pop();
if (temp.x == ex && temp.y == ey) {
printf("%d\n",temp.step);
break;
}
for (int i = 0; i < 8; i++) {
next.x = temp.x + Move[i][0];
next.y = temp.y + Move[i][1];
if(check(next))
continue;
else{
pos[next.x][next.y] = 1;
next.step = temp.step + 1;
q.push(next);
}
}
}
}
int main() {
//freopen("input.txt", "r", stdin);
scanf("%d",&T);
while(T--) {
memset(pos,0,sizeof(pos));
scanf("%d",&l);
scanf("%d %d",&sx,&sy);
scanf("%d %d",&ex,&ey);
dfs();
}
return 0;
}
B:Curling 2.0(POJ3009)
具体题目见这里:
http://www.cnblogs.com/ACShiryu/archive/2011/07/23/2114718.html
题目大意就是给出一个w*h的地图,其中0代表空地,1代表障碍物,2代表起点,3代表终点,每次行动可以走多个方格,每次只能向附近一格不是障碍物的方向行动,直到碰到障碍物才停下来,此时障碍物也会随之消失,如果行动时超出方格的界限或行动次数超过了10则会game over .如果行动时经过3则会win,记下此时行动次数(不是行动的方格数),求最小的行动次数
我的代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
using namespace std;
int w,h;
int minStep;
struct prog{
int Map[25][25];
int x,y;
};
int Move[4][2]= {{-1,0},{1,0},{0,1},{0,-1}};
void dfs(prog x, int k){
int step = k;
if (step >= minStep)
return;
for (int i = 0; i < 4; i ++) {
if (x.x+Move[i][0] >= 0 && x.x + Move[i][0] < h && x.y+Move[i][1] >= 0 && x.y + Move[i][1] < w&& x.Map[x.x+Move[i][0]][x.y+Move[i][1]] != 1) {
for (int j = 1; x.x+j*Move[i][0] >= 0 && x.x + j*Move[i][0] < h && x.y+j*Move[i][1] >= 0 && x.y + j*Move[i][1] < w; j++) {
if (x.Map[x.x+j*Move[i][0]][x.y+j*Move[i][1]] == 3) {
if (k+1 < minStep) {
minStep = k+1;
}
break;
}else if(x.Map[x.x+j*Move[i][0]][x.y+j*Move[i][1]] == 1){
prog temp = x;
temp.Map[x.x+j*Move[i][0]][x.y+j*Move[i][1]] = 0;
temp.x = x.x+(j-1)*Move[i][0];
temp.y = x.y+(j-1)*Move[i][1];
dfs(temp,k+1);
break;
}
}
}
}
}
int main() {
// freopen("input.txt", "r", stdin);
prog a;
while(scanf("%d%d",&w,&h)!=EOF) {
if (w == 0 && h == 0) {
break;
}else{
minStep = 11;
memset(&a,0,sizeof(a));
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
scanf("%d",&a.Map[i][j]);
if (a.Map[i][j] == 2) {
a.x = i;a.y = j;
}
}
}
dfs(a,0);
if (minStep > 10) {
printf("-1\n");
}else{
printf("%d\n",minStep);
}
}
}
return 0;
}
E:Channel Allocation(POJ1129)
题目:
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.
Input:
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.
Output
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.
Sample Input
2
A:
B:
4
A:BC
B:ACD
C:ABD
D:BC
4
A:BCD
B:ACD
C:ABD
D:ABC
0
Sample Output
1 channel needed.
3 channels needed.
4 channels needed.
我的解答
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int INF = 1000000000;
const int maxn = 100;
int nodenum;
int Map[30][30];
int color[30];
char s[40];
bool isfind;
int colorall;
bool ok(int nodei, int c){
for (int i = 0; i < nodenum; i ++) {
if (Map[nodei][i] && color[i] == c) {
return false;
}
}
return true;
}
void dfs(int nodei, int colorn){
if (isfind == true){
return;
}
if (nodei >= nodenum ) {
isfind = true;
return;
}
for (int i = 1; i <= colorn ; i++) {
if (ok(nodei,i)) {
color[nodei] = i;
dfs(nodei+1,colorn);
color[nodei] = 0;
}
}
if (!isfind) {
colorall++;
dfs(nodei,colorn+1);
}
}
int main() {
// freopen("input.txt", "r", stdin);
while(scanf("%d",&nodenum)!=EOF && nodenum != 0){
getchar();
memset(Map,0,sizeof(Map));
memset(color,0,sizeof(color));
for (int i = 0; i < nodenum; i++) {
gets(s);
for (int j = 2; s[j]!='\0'; j++) {
Map[s[0]-'A'][s[j]-'A'] = 1;
}
}
isfind = false;
colorall = 1;
dfs(0,1);
if (colorall == 1) {
printf("1 channel needed.\n");
}else{
printf("%d channels needed.\n",colorall);
}
}
return 0;
}