zoukankan      html  css  js  c++  java
  • Journey Codeforces 34D

    题目链接 http://codeforces.com/problemset/problem/43/D

    D. Journey
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    The territory of Berland is represented by a rectangular field n × m in size. The king of Berland lives in the capital, located on the upper left square (1, 1). The lower right square has coordinates (n, m). One day the king decided to travel through the whole country and return back to the capital, having visited every square (except the capital) exactly one time. The king must visit the capital exactly two times, at the very beginning and at the very end of his journey. The king can only move to the side-neighboring squares. However, the royal advise said that the King possibly will not be able to do it. But there is a way out — one can build the system of one way teleporters between some squares so that the king could fulfill his plan. No more than one teleporter can be installed on one square, every teleporter can be used any number of times, however every time it is used, it transports to the same given for any single teleporter square. When the king reaches a square with an installed teleporter he chooses himself whether he is or is not going to use the teleport. What minimum number of teleporters should be installed for the king to complete the journey? You should also compose the journey path route for the king.

    Input

    The first line contains two space-separated integers n and m (1 ≤ n, m ≤ 100, 2 ≤  n · m) — the field size. The upper left square has coordinates (1, 1), and the lower right square has coordinates of (n, m).

    Output

    On the first line output integer k — the minimum number of teleporters. Then output k lines each containing 4 integers xyxy2(1 ≤ x1, x2 ≤ n, 1 ≤ y1, y2 ≤ m) — the coordinates of the square where the teleporter is installed (x1, y1), and the coordinates of the square where the teleporter leads (x2, y2).

    Then print nm + 1 lines containing 2 numbers each — the coordinates of the squares in the order in which they are visited by the king. The travel path must start and end at (1, 1). The king can move to side-neighboring squares and to the squares where a teleporter leads. Besides, he also should visit the capital exactly two times and he should visit other squares exactly one time.

    Examples
    input
    2 2
    output
    0
    1 1
    1 2
    2 2
    2 1
    1 1
    input
    3 3
    output
    1
    3 3 1 1
    1 1
    1 2
    1 3
    2 3
    2 2
    2 1
    3 1
    3 2
    3 3
    1 1

    模拟即可,要特判几种情况

    #include<cstdio>
    #include<algorithm>
    #include<map>
    #include<cstring>
    #include<iostream>
    #include<queue>
    using namespace std;
    int main()
    {
        int n,m;
        scanf("%d%d",&n,&m);
        if(n==1&&m==2)
        {
            printf("0
    1 1
    1 2
    1 1
    ");
        }
        else if(n==2&&m==1)
        {
            printf("0
    1 1
    2 1
    1 1
    ");
        }
        else if(n==1&&m>1)
        {
            printf("1
    ");
            printf("1 %d 1 1
    ",m);
            for(int i=1;i<=m;i++)
            printf("1 %d
    ",i);
            printf("1 1
    ");
        }
        else if(m==1&&n>1)
        {
            printf("1
    ");
            printf("%d 1 1 1
    ",n);
            for(int i=1;i<=n;i++)
            printf("%d 1
    ",i);
            printf("1 1
    ");
        }
        else if(m==3&&n==3)
        {
            printf("1
    3 3 1 1
    1 1
    1 2
    1 3
    2 3
    2 2
    2 1
    3 1
    3 2
    3 3
    1 1
    ");
        }
        else if(n==2&&m==2)
        {
            printf("0
    1 1
    1 2
    2 2
    2 1
    1 1
    ");
        }
        else if(n%2==0&&m>=3)
        {
    
            printf("0
    1 1
    ");
            for(int l=1;l<=n;l++)
            {
                if(l%2)
                {
                    for(int j=2;j<=m;j++)
                        printf("%d %d
    ",l,j);
                }
                else
                {
                    for(int j=m;j>=2;j--)
                        printf("%d %d
    ",l,j);
                }
            }
            for(int i=n;i>=1;i--)
                printf("%d 1
    ",i);
    
        }
        else if(m%2==0&&n>=3)
        {
            printf("0
    1 1
    ");
            for(int l=1;l<=m;l++)
            {
                if(l%2)
                {
                    for(int j=2;j<=n;j++)
                        printf("%d %d
    ",j,l);
                }
                else
                {
                    for(int j=n;j>=2;j--)
                        printf("%d %d
    ",j,l);
                }
            }
            for(int i=m;i>=1;i--)
                printf("1 %d
    ",i);
    
        }
        else if(n%2&&m%2)
        {
            printf("1
    ");
            printf("%d %d 1 1
    ",n,m);
            for(int i=1;i<=n;i++)
            {
                if(i%2)
                {
                    for(int j=1;j<=m;j++)
                        printf("%d %d
    ",i,j);
                }
                else
                {
                    for(int j=m;j>=1;j--)
                        printf("%d %d
    ",i,j);
                }
    
            }
            printf("1 1
    ");
    
    
        }
    
    
        return 0;
    }
  • 相关阅读:
    (十三)子查询
    (十二)多表查询
    MFC读写配置ini文件
    (十一)分组函数(多行函数)
    Django(二十一)组合搜索
    Django(二十)model中的 class Meta
    (十)单行函数
    (九)逻辑运算,order by,desc
    类作为成员变量
    内部类——匿名内部类
  • 原文地址:https://www.cnblogs.com/longl/p/7301547.html
Copyright © 2011-2022 走看看