罗勇军 → 《算法竞赛·快冲300题》每日一题:“窗户”

【题目来源】
http://oj.ecustacm.cn/problem.php?id=1706
http://oj.ecustacm.cn/viewnews.php?id=1023
https://blog.csdn.net/weixin_43914593/category_12377073.html

【题目描述】
有一张宿舍窗户的照片,请你判断有多少种不同款式的窗户。为了简化问题,作如下定义:
1. 输入图片表示为一个 n 行 m 列的字符矩阵
2. 窗户形状为一个矩形,输入保证在同一张图片中,
所有的窗户有相同的尺寸
3. 输入图片中 “#” 表示墙壁,“.” 和 “+” 表示窗户的不同花纹
4. 输入保证图片的最外层为一层 “#”,两个相邻的窗户之间也只有一层 “#”
5. 在同一张图片中,
每一行窗户数量是一样的,每一列窗户数量也是一样的
6. 一个窗户至少包含一个单元格
7. 不同的输入图片,图片本身的尺寸、窗户的尺寸都是任意的
款式一样:代表窗户的字符矩阵完全一致,或者
旋转90、180、270度后完全一致,则表示这两个窗户是一样的款式。
请你根据输入图片,判断有多少种不同款式的窗户。

【输入格式】
输入第一行包含两个正整数 n,m(
1≤n,m≤120)。接下来 n 行,每行 m 个字符,表示输入图片尺寸。

【输出格式】
输出一个数字,表示有多少种不同款式的窗户。

【输入样例】

11 16
################
#....#++++#+...#
#....#++.+#+...#
#....#.++.#++.+#
#....#....#++++#
################
#....#.+..#++++#
#..++#.+..#++.+#
#+...#....#.++.#
#+...#..++#....#
################

【输出样例】
4

【算法分析】
(1)本题是一道较为细致的模拟题,主要工作有两个:一是
旋转窗户,二是去重
** 旋转窗户。根据题意,一个窗户虽然在旋转90度、180度、270度后将展现为不同的形态,但它仍为一种窗户。如何唯一地表示一个给定的窗户呢?代码中,通过自定义函数 find() 将一个给定窗户由于旋转而生成的 4 中形态都
编码为字符串,然后比较选择最小的,存储在字符串 cur 中,这样一个给定的窗户就唯一确定了。
字符串比较大小的示例代码如下:

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

int main(){
    string s,t;
    cin>>s>>t;
    if(s>=t) cout<<"1"<<endl;
    else cout<<"0"<<endl;
    
    return 0;
}

/*
in:
NanJing Beijing 

out:
1
*/

** 去重。把所有窗户完成字符串编码后,都插入到名字为 win 的 STL set 中,而 STL set 具有自动去重的功能,从而得到不同款式窗户的数量。其可通过调用函数 win.size() 得到。

(2)代码中 swap(w,h) 函数的解析
若以 w×h=2×3 的矩阵为例,则调用函数 rotate(w,h) 后,将会变为 3×2 的矩阵。但是,分析函数 rotate(w,h) 后发现,它并没有改变 w 和 h 的值,所以为了体现 w 变为 h,h 变为 w 的这种变化,就可以通过调用函数  swap(w,h) 来实现。

【算法代码】

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

const int maxn=125;
char w[maxn][maxn]; //map
char c[maxn][maxn]; //one window
int n,m;

void rotate(int w,int h) { //rotate window 90 ClockWise
    char tmp[maxn][maxn];
    for(int i=1; i<=w; i++)
        for(int j=1; j<=h; j++)
            tmp[i][j]=c[i][j];

    for(int i=1; i<=w; i++)
        for(int j=1; j<=h; j++)
            c[j][w+1-i]=tmp[i][j]; //rotate window 90 ClockWise
}

string find(int x1, int y1) { //(x1, y1): upper left coordinates of sub-map
    int x2=x1, y2=y1; //(x2, y2): Bottom right coordinates of sub-map
    while(x2+1<=n && w[x2+1][y1]!='#') x2++;
    while(y2+1<=m && w[x1][y2+1]!='#') y2++;

    for(int i=x1; i<=x2; i++) { //copy sub-map to window c[][]
        for(int j=y1; j<=y2; j++) {
            c[i-x1+1][j-y1+1]=w[i][j];
        }
    }

    string ans;
    int w=x2-x1+1, h=y2-y1+1; //window's width and height
    for(int t=1; t<=4; t++) { //rotate window four times
        string cur;
        for(int i=1; i<=w; i++) { //change window's char into string cur
            for(int j=1; j<=h; j++) {
                cur+=c[i][j];
            }
        }
        if(ans.size()==0 || cur<ans) ans=cur;
        rotate(w,h); //rotate window 90 ClockWise
        swap(w,h); //swap width and height after rotating
    }
    return ans;
}

int main() {
    cin>>n>>m;
    for(int i=1; i<=n; i++) {
        for(int j=1; j<=m; j++) {
            cin>>w[i][j]; //input map
        }
    }
    //for(int i=1; i<=n; i++) cin>>w[i];
    
    set<string> win; //record all the windows and deduplication
    for(int i=1; i<=n; i++) {
        for(int j=1; j<=m; j++) { //position a window, top left, left and top are '#'
            if(w[i-1][j-1]=='#' && w[i][j-1]=='#' && w[i-1][j]=='#') {
                auto t=find(i,j);
                win.insert(t);
            }
        }
    }
    cout<<win.size()<<endl; //number of windows

    return 0;
}



/*
in:
11 16
################
#....#++++#+...#
#....#++.+#+...#
#....#.++.#++.+#
#....#....#++++#
################
#....#.+..#++++#
#..++#.+..#++.+#
#+...#....#.++.#
#+...#..++#....#
################

out:
4
*/




【参考文献】
http://oj.ecustacm.cn/viewnews.php?id=1023
https://blog.csdn.net/weixin_43914593/category_12377073.html
https://blog.csdn.net/weixin_43914593/article/details/131699593




 

猜你喜欢

转载自blog.csdn.net/hnjzsyjyj/article/details/132217117