Chapaev and Potatoes (URAL 1809 暴力)

Chapaev and Potatoes

Time Limit: 500MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u

Submit Status

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 xiyi 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;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-09-21 02:44:08

Chapaev and Potatoes (URAL 1809 暴力)的相关文章

Ural 1780 Gray Code (暴力)

Ural 1780 题意: 输入一个二进制数与该二进制数的格雷码,但有一些位置不确定,写程序将其恢复,无法恢复输出Impossible,多解输出Ambiguity. 思路: 其实是个普通的乱搞题.. Gray码的定义:Gi=Bi?1?Bi,G0=B0 (第i位格雷码等于第i位二进制码与第i-1位二进制码的异或值) 然后按照这个定义编码,正向反向各自编一遍,出现矛盾输出Impossible,最后还含有'?'则有多解. 代码: /* * @author FreeWifi_novicer * lang

ural 1104. Don’t Ask Woman about Her Age暴力

1104. Don’t Ask Woman about Her Age Time limit: 1.0 secondMemory limit: 64 MB Mrs Little likes digits most of all. Every year she tries to make the best number of the year. She tries to become more and more intelligent and every year studies a new di

ural 1104,暴力取模

题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1104 题目大意:输入一个字符串(数字与大写字母组成),输出n,n满足此字符串为n进制时,其n进位制数能被n-1整除(n不存在时输出"No solution"(不包括双引号)). 题目好多坑点,我也是WA了好几次才A的.算法是暴力的. 1,刚开始,我怎么都想不清楚,答案就是2啊,任何数都可以被1整除啊,其实,不是这样的,比如说,123就不是2进制数,所以说,这里要找到字符串中最

URAL 2066 Simple Expression (水题,暴力)

题意:给定三个数,让你放上+-*三种符号,使得他们的值最小. 析:没什么好说的,全算一下就好.肯定用不到加,因为是非负数. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream>

Ural 1780 Gray Code 乱搞暴力

原题链接:http://acm.timus.ru/problem.aspx?space=1&num=1780 1780. Gray Code Time limit: 0.5 secondMemory limit: 64 MB Denis, Vanya and Fedya gathered at their first team training. Fedya told them that he knew the algorithm for constructing aGray code. Cre

ural 1217. Unlucky Tickets

1217. Unlucky Tickets Time limit: 1.0 secondMemory limit: 64 MB Strange people live in Moscow! Each time in the bus, getting a ticket with a 6-digit number, they try to sum up the first half of digits and the last half of digits. If these two sums ar

ural 1249. Ancient Necropolis

1249. Ancient Necropolis Time limit: 5.0 secondMemory limit: 4 MB Aerophotography data provide a bitmap picture of a hard-to-reach region. According to the suggestions of scientists, this region is a cemetery of an extinct civilization. Indeed, the p

ural 1071. Nikifor 2

1071. Nikifor 2 Time limit: 1.0 secondMemory limit: 64 MB Nikifor has a number x. He doesn't need it. He needs a number y. Nikifor tries to obtain the required number by erasing some digits from x. But he is not lucky in the meanwhile. May be he is t

ural 1070. Local Time

1070. Local Time Time limit: 1.0 secondMemory limit: 64 MB Soon the USU team will go to Vancouver to participate in the final of the ACM International Collegiate Programming Contest. They will be to take four different planes (three changes on the wa