【习题 4-2 Uva201】Squares

【链接】 我是链接,点我呀:)
【题意】

在这里输入题意

【题解】

注意那个星号的数量。。。
然后V x y的话,是从(y,x)向(y+1,x)连线。
H x y才是从(x,y)向(x,y+1)连线
枚举以(x,y)作为左上角的举行就ok了

【代码】

#include <bits/stdc++.h>
using namespace std;

const int N = 10;

int n,m;
bool a[N+10][N+10][2];
int cnt[N+5];

bool check(int x,int y,int p){
    for (int i = 1;i <= p;i++)
        if (a[x][y][0]){
            y++;
            if (y>n) break;
        }else return 0;

    for (int i = 1;i <= p;i++)
        if (a[x][y][1]){
            x++;
            if (x>n) break;
        }else return 0;

    for (int i = 1;i <= p;i++)
        if (y-1>=1 && a[x][y-1][0]){
            y--;
            if (y<1) break;
        }else return 0;

    for (int i = 1;i <= p;i++)
        if (x-1>=1 && a[x-1][y][1]){
            x--;
            if (x<1) break;
        }else return 0;
    return 1;
}

int main(){
    //freopen("/home/ccy/rush.txt","r",stdin);
    //freopen("/home/ccy/rush_out.txt","w",stdout);
    ios::sync_with_stdio(0),cin.tie(0);
    int kase = 0;
    while (cin >> n){
        if (kase>0){
            cout<<endl;
            cout<<"**********************************"<<endl;
            cout<<endl;
        }
        cout<<"Problem #"<<++kase<<endl<<endl;
        memset(a,0,sizeof a);
        memset(cnt,0,sizeof cnt);
        cin >> m;
        for (int i = 1;i <= m;i++){
            char s[5];int x,y;
            cin >> s >> x >> y;

            if (s[0]=='H')
                a[x][y][0] = 1;
            else{
                swap(x,y);
                a[x][y][1] = 1;
            }
        }
        for (int p = 1;p <= n;p++)
            for (int i = 1;i <= n;i++)
                for (int j = 1;j <= n;j++)
                    if (check(i,j,p))
                        cnt[p]++;
        bool none = 1;
        for (int i = 1;i <= n;i++){
            if (cnt[i]>0) {
                none = 0;
                cout<<cnt[i]<<" square (s) of size "<<i<<endl;
            }

        }
        if (none) cout<<"No completed squares can be found."<<endl;
    }
    return 0;
}

原文地址:https://www.cnblogs.com/AWCXV/p/9855185.html

时间: 2024-08-03 09:36:15

【习题 4-2 Uva201】Squares的相关文章

uva201 Squares

 Squares  A children's board game consists of a square array of dots that contains lines connecting some of the pairs of adjacent dots. One part of the game requires that the players count the number of squares of certain sizes that are formed by the

【习题 7-6 UVA - 12113】Overlapping Squares

[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 先预处理出来一个正方形. 然后每次枚举新加的正方形左上角的坐标就可以. 注意覆盖的规则,控制一下就可以. 然后暴力判断是否相同. 暴力回溯即可(只用回溯一个正方形区域) [代码] /* 1.Shoud it use long long ? 2.Have you ever test several sample(at least therr) yourself? 3.Can you promise that the solutio

c primer plus--C控制语句:循环(第6章)--习题

本章习题有点难,还有几题没做,等有空的时候再补上吧. (对于题的想法会放在题后面或代码注释里) 1.编写一个程序,创建一个具有 26 个元素的数组,并在其中存储 26 个小写字母.并让该程序显示该数组的内容. 1 #include <stdio.h> 2 #define SIZE 26 3 int main(void) 4 { 5 int arry[SIZE]; 6 int i; 7 char c='a'; 8 for(i=0; i<SIZE; i++, c++) 9 { 10 prin

数据库经典习题,

/* 数据导入: Navicat Premium Data Transfer Source Server : localhost Source Server Type : MySQL Source Server Version : 50624 Source Host : localhost Source Database : sqlexam Target Server Type : MySQL Target Server Version : 50624 File Encoding : utf-8

C/C++算法竞赛入门经典Page15 习题1-1 平均数

题目:输入3个整数,输出他们的平均值,保留3位小数. 首先,声明三个整数a,b,c和一个浮点数d: int a,b,c; double d; 输入三个整数a,b,c: scanf("%d%d%d",&a,&b,&c); 将a,b,c取平均值以后复制给d: d=(double)(a+b+c)/3; 最后输出d: printf("%.3lf",d); %.3lf表示保留3位小数的long float. 注意:不能直接这样输出: printf(&q

问题 1018: C语言程序设计教程(第三版)课后习题6.8

/******************************************************************** @file Main.cpp @date 2017-05-12 @author Zoro_Tiger @brief 问题 1018: C语言程序设计教程(第三版)课后习题6.8 http://www.dotcpp.com/oj/problem1018.html *************************************************

USACO 1.2 Palindromic Squares

Palindromic SquaresRob Kolstad Palindromes are numbers that read the same forwards as backwards. The number 12321 is a typical palindrome. Given a number base B (2 <= B <= 20 base 10), print all the integers N (1 <= N <= 300 base 10) such that

SICP 习题 (1.46)解题总结

SICP 习题 1.46 要求我们写一个过程iterative-improve,它以两个过程为参数,其中一个参数用来检测猜测是否足够好,另一个参数用来改进猜测.过程iterative-improve应该返回另一个过程,所返回的过程接收一个参数作为初始猜测,然后不断改进猜测直到结果足够好.题目还要求我们使用iterative-improve重写1.1.7的sqrt过程和1.3.3节的fixed-point过程. 因为涉及到高阶函数,所以整个题目理解起来有一点点费劲.不过这道题作为第一章的收官题确实

Partial least squares regression(偏最小二乘法回归)

偏最小二乘法(PLS)是近年来发展起来的一种新的多元统计分析 http://en.wikipedia.org/wiki/Partial_least_squares_regression Partial least squares regression(偏最小二乘法回归),布布扣,bubuko.com