20200201 POJ-3279Fliptile POJ - 2676 Sudoku

>>>>>>>>>POJ 3279直达🚗

>>>>>>>>>POJ 2676直达🚗

 

 

9:45:24      这两天的总结都是第二天发布的 害 娱乐时间总是不会想起干正事的

11:25:33    决定从这一篇起加上题目标题(好像什么水题都能拿得出手一样

13:55:23    哈哈哈哈哈哈哈为了捣鼓这个学了个新东西 妙啊(HTML

18:29:14    哈哈哈哈哈哈哈哈哈一次性AC了写了一下午的东西  我又活了

 

今天是非常快乐的总结:

  先是自己的小结:

    1.翻牌其实是简单的递归思想,把自己搞懵了(不过据说是叫关灯问题

    2.如果会位运算,翻牌这个题应该会更简单,等会看看

    3.数独有很多重复操作,其实可以用循环优化

    4.数独一开始忘记填过要标记了

  接着是观大佬总结有感:(等我看了再来写😀


    1.不同的代码块可以用花括号括起

    2.位运算好重要喔

    3.写个rep太便捷了,学到了学到了

 

 

 

===================================分割线===================================

 POJ-3279

有时间再修改   没时间就算哒  将就着看吧

#include <iostream>
#include <cstring>
#include <queue>
#include <math.h>
#include <map>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=20;
int a[maxn][maxn],o[maxn][maxn],out[maxn][maxn],path[maxn][maxn],mins=1e9;
//int line[10][10],row[10][10],block[10][10];
int n,m,flag,coun;
//这堆乱七八糟的是bfs用的
struct point{int l,x,y;};
typedef pair<point,int>p;
int dx[]={0,0,-1,1};
int dy[]={1,-1,0,0};
point now,newo,ende;
queue<p> qu;
//这以上忽略忽略
inline int read(){
    int x=0,f=1;char c=getchar();
    while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
    return x*f;
}

inline void clean(){//在这没用,毕竟就一组数据
    memset(a,0,sizeof(a));
    memset(out,0,sizeof(out));
}

void print(){//为了查错写的,可忽略
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++)
            cout<<path[i][j];
        cout<<endl;
    }
}

void print1(){//输出结果
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++)
            cout<<out[i][j]<<" ";
        cout<<endl;
    }
}

int judge(){
    coun=0;
    //我可能是个傻子,之前直接对a操作,输出结果后 我:????
    memcpy(a,o,sizeof(o));
    for(int r=1;r<=n;r++)
    {
        for(int i=1;i<=m;i++)
        {
            if(path[r][i])
            {

                a[r-1][i]=!a[r-1][i];
                a[r][i-1]=!a[r][i-1];
                a[r][i+1]=!a[r][i+1];
                a[r][i]=!a[r][i];
                a[r+1][i]=!a[r+1][i];
                coun++;
            }//如果前一行的操作不足,后续补齐
             //这个之前也写在前面的判断语句里了,傻了已经
            if(a[r][i-1])path[r+1][i-1]=1;
            else path[r+1][i-1]=0;
            //getchar();
            //print();cout<<endl;查错用的,忽略
        }
        if(a[r][m])path[r+1][m]=1;
        else path[r+1][m]=0;
    }//执行完所有操作
    //print();
    for(int i=1;i<=m;i++)
        if(a[n][i])return 0;
    return 1;//判断最后一行
}

void dfs(int p){
    if(p>-1)
    {
        int temp=p;
        for(int i=m;i>=1;i--)
        {
            path[1][i]=temp%2;
            temp/=2;
        }
        if(judge())
        {
            flag=1;
            if(coun<mins)
            {
                mins=coun;
                memcpy(out,path,sizeof(out));
            }
        }//全是查错的,不管
//        cout<<p<<endl;
//        cout<<"path:\n";
//        print();cout<<endl;
//        cout<<"out:\n";
//        print1();cout<<endl;
//        getchar();
        if(p==0 && flag){print1();};
        dfs(p-1);
    }
    if(p==-1 && !flag)printf("IMPOSSIBLE\n");
}

int main()
{
	n=read(),m=read();
	char ll[5];
	for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            scanf("%s",ll);
            o[i][j]=ll[0]-'0';
        }
	}//输入
    dfs(1<<m);
	return 0;
}

  

 

===================================分割线===================================

POJ - 2676

#include <iostream>
#include <cstring>
#include <queue>
#include <math.h>
#include <map>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
int a[10][10];
int line[10][10],row[10][10],block[10][10];
int n,m,flag;
struct point{int l,x,y;};
typedef pair<point,int>p;
int dx[]={0,0,-1,1};
int dy[]={1,-1,0,0};
point now,newo,ende;
queue<p> qu;
inline int read(){
    int x=0,f=1;char c=getchar();
    while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
    return x*f;
}

inline void clean(){//清空
    memset(a,0,sizeof(a));
    memset(line,0,sizeof(line));
    memset(row,0,sizeof(row));
    memset(block,0,sizeof(block));
    flag=0;
}

int go(int i,int j){//判断块
    if(i>0 && i<4 && j>0 && j<4)return 1;
    if(i>3 && i<7 && j>0 && j<4)return 2;
    if(i>6 && i<10 && j>0 && j<4)return 3;
    if(i>0 && i<4 && j>3 && j<7)return 4;
    if(i>3 && i<7 && j>3 && j<7)return 5;
    if(i>6 && i<10 && j>3 && j<7)return 6;
    if(i>0 && i<4 && j>6 && j<10)return 7;
    if(i>3 && i<7 && j>6 && j<10)return 8;
    return 9;
}

void print(){
    for(int i=1;i<=9;i++){
        for(int j=1;j<=9;j++)
            cout<<a[i][j];
        cout<<endl;
    }
}

void dfs(int i,int j){
//这个flag是为了跳出去{不能多余答案输出,不然就OLE或者TLE🆒),但是鉴于void无法返回,只能立flag了,但是看着好蠢
    if(!flag){
        if(i==9){
            if(j==9 && a[i][j])
                j++;//这个操作也很蠢,但是不知道怎么优化,之后再看看大佬写的吧
            if(j==10)
                {print();flag=1;}
        }
        if(j>9){i++,j=1;}
        if(!a[i][j]){
            for(int p=1;p<=9;p++){
                if(!row[i][p] && !line[j][p] && !block[go(i,j)][p]){
                    a[i][j]=p,line[j][p]=1,row[i][p]=1,block[go(i,j)][p]=1;
                    dfs(i,j+1);
                    a[i][j]=0,row[i][p]=0,line[j][p]=0,block[go(i,j)][p]=0;//复原操作
                }
            }
            return;
        }
        dfs(i,j+1);
    }
}

int main()
{
	n=read();
	char ll;
	int temp;
	while(n--){
        clean();
        for(int j=1;j<=9;j++){
            for(int i=1;i<=9;i++){
                ll=getchar();
                temp=ll-'0';
                a[j][i]=temp;line[i][temp]=1;row[j][temp]=1;block[go(j,i)][temp]=1;//标记行列块
            }
            ll=getchar();
        }
        dfs(1,1);
	}
	return 0;
}

 

 

/*代码高亮*/
.syntaxhighlighter .bold {
    font-weight:unset !important;
}
.syntaxhighlighter .line {
    background-color: rgb(40, 43, 46)!important;
}
.syntaxhighlighter .line.alt2 {
    background-color: rgb(40, 43, 46)!important;
}
.syntaxhighlighter .line.alt1 {
    background-color: rgb(40, 43, 46)!important;
}
.syntaxhighlighter .comments, .syntaxhighlighter .comments a {
    color: rgb(129, 142, 150)!important;
}
.syntaxhighlighter .keyword {
    color: rgb(147, 199, 99)!important;
    font-weight: unset !important;
}
.syntaxhighlighter .preprocessor {
    color: rgb(85, 113, 130) !important;
}
.syntaxhighlighter .plain, .syntaxhighlighter .plain a {
    color: rgb(224, 226, 228)!important;
}
.syntaxhighlighter .color1, .syntaxhighlighter .color1 a {
    color: rgb(147, 199, 99)!important;
}
.syntaxhighlighter .string, .syntaxhighlighter .string a {
    color: rgb(236, 118, 0)!important;
}
.syntaxhighlighter .functions {
    color: rgb(140, 187, 173)!important;
}
.syntaxhighlighter .gutter .line {
    border-right: 2px solid rgb(147, 199, 99)!important;
}
.syntaxhighlighter.collapsed .toolbar {
    background: rgb(40, 43, 46)!important;
    border: none !important;
    border-radius: 5px !important;
}
.syntaxhighlighter.collapsed .toolbar a {
    color: rgb(147, 199, 99)!important;
}
.syntaxhighlighter.collapsed .toolbar a:hover {
    color: rgb(78, 109, 48)!important;
}
.syntaxhighlighter {
    border-radius: 5px;
}
/*第一种高亮结束,第二种高亮开始*/
.cnblogs_code div {
    background: #282b2e;
}
.cnblogs_code {
    background: #282b2e;
    border-radius: 5px;
    border: none;
    font-family: consolas !important;
    color: #fff;
}
.cnblogs_code_toolbar {
    background: #282b2e !important;
}
.cnblogs_code_toolbar a:hover, .cnblogs_code_toolbar a:link, .cnblogs_code_toolbar a:visited, .cnblogs_code_toolbar a:active, .cnblogs_code_toolbar a:link img, .cnblogs_code_toolbar a:visited img {
    background-color: #282b2e !important;
    border: none!important;
}
.cnblogs_code pre {
    font-family: consolas !important;
    padding-left: 3px;
    color: rgb(224, 226, 228);
}
.cnblogs_code span[style="color: #000000;"] , .cnblogs_code span[style="color: #ff0000;"]{
    color: rgb(224, 226, 228) !important;
}
.cnblogs_code span[style="color: #0000ff;"]{
    color: rgb(147, 199, 99)!important;
}
.cnblogs_code span[style="color: #800080;"]{
    color: #ffd740 !important;
}
.cnblogs_code span[style="color: #800000;"]{
    color: rgb(236, 118, 0)!important;
}
.cnblogs_code span[style="color: #008000;"]{
    color: rgb(129, 142, 150)!important;
}
.cnblogs_code span[style="color: #008080;"]{
    color: #afafaf!important;
    margin-right: 5px;
}
.cnblogs_code_collapse {
    border: none;
    background: #282b2e;
    color: rgb(147, 199, 99);
}
.cnblogs_code > pre {
    border: none !important;
}
.cnblogs_code > textarea {
    color: #fff;
    background: transparent;
    border: none;
    outline: none;
}
/*代码高亮结束*/
View Code

 

posted @ 2020-02-01 12:23  Tabshh  阅读(175)  评论(0)    收藏  举报