Time Limit: 500MS | Memory Limit: 65536KB | 64bit IO Format: %I64d & %I64u |
Description
Anka and Petka were waiting for Chapaev and eating potatoes. Soon they were full and decided to play the “Chapaev” game using the remaining four potatoes.
Petka took a 20 × 20 board, put the potatoes on it, and declared the following rules. No two potatoes could lie on the same square, and a player could shoot at a potato and knock it off the board with another potato only if the
potatoes were in the same vertical or horizontal line and there were no other potatoes between them.
Anka suggested to take some potatoes and put them on unoccupied squares of the board so that each potato could be used to shoot at exactly one another potato. Help Petka do this by changing the positions of as few potatoes as
possible.
Input
The four input lines contain the coordinates xi, yi of the potatoes. The coordinates are integers in the range from 1 to 20. No two potatoes are on the same square.
Output
Output the new coordinates of the potatoes. The potatoes must be described in the same order as in the input. If there are several answers, output any of them.
Sample Input
input | output |
---|---|
1 1 2 2 4 4 4 3 |
1 2 2 2 4 4 4 3 |
Source
Problem Author: Dmitry Ivankov
Problem Source: NEERC 2010, Eastern subregional contest
题意:四个土豆在20*20的方格上,告诉起始位置,要求改变某些土豆的位置使得每个土豆只有一个土豆与他在同一行或同一列,要求改变的次数尽量小。
代码:
#include <iostream> #include <functional> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <string> #include <map> #include <stack> #include <vector> #include <set> #include <queue> #pragma comment (linker,"/STACK:102400000,102400000") #define pi acos(-1.0) #define eps 1e-6 #define lson rt<<1,l,mid #define rson rt<<1|1,mid+1,r #define FRE(i,a,b) for(i = a; i <= b; i++) #define FREE(i,a,b) for(i = a; i >= b; i--) #define FRL(i,a,b) for(i = a; i < b; i++) #define FRLL(i,a,b) for(i = a; i > b; i--) #define mem(t, v) memset ((t) , v, sizeof(t)) #define sf(n) scanf("%d", &n) #define sff(a,b) scanf("%d %d", &a, &b) #define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c) #define pf printf #define DBG pf("Hi\n") typedef long long ll; using namespace std; #define INF 0x3f3f3f3f #define mod 1000000009 const int maxn = 1005; const int MAXN = 2005; const int MAXM = 200010; const int N = 1005; struct Node { int x,y; }node[5]; bool vis[25][25]; int a[5]; bool flag; bool isok() { memset(a,0,sizeof(a)); for (int i=0;i<4;i++) { for (int j=0;j<4;j++) { if (i==j) continue; if (node[i].x==node[j].x||node[i].y==node[j].y) a[i]++; } } for (int i=0;i<4;i++) if (a[i]!=1) return false; return true; } void dfs(int step,int sum) { if (flag) return ; if (step==sum) { if (isok()) { flag=true; for (int i=0;i<4;i++) printf("%d %d\n",node[i].x,node[i].y); } return ; } for (int k=0;k<4;k++) { int x=node[k].x; int y=node[k].y; for (int i=1;i<=20;i++) { for (int j=1;j<=20;j++) { if (vis[i][j]) continue; vis[x][y]=false; vis[i][j]=true; node[k].x=i;node[k].y=j; dfs(step+1,sum); vis[x][y]=true; vis[i][j]=false; node[k].x=x;node[k].y=y; } } } return ; } int main() { #ifndef ONLINE_JUDGE freopen("C:/Users/lyf/Desktop/IN.txt","r",stdin); #endif int i,j; while (~scanf("%d%d",&node[0].x,&node[0].y)) { memset(vis,false,sizeof(vis)); for (i=1;i<4;i++) { scanf("%d%d",&node[i].x,&node[i].y); vis[node[i].x][node[i].y]=true; } flag=false; for (i=0;i<3;i++) { if (flag) break; dfs(0,i); } } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。