POJ 1830 开关问题

开关问题
Time Limit: 1000MS      Memory Limit: 30000K
Total Submissions: 7739     Accepted: 3037

DescriptionN个相同的开关,每个开关都与某些开关有着联系,每当你打开或者关闭某个开关的时候,其他的与此开关相关联的开关也会相应地发生变化,即这些相联系的开关的状态如果原来为开就变为关,如果为关就变为开。你的目标是经过若干次开关操作后使得最后N个开关达到一个特定的状态。对于任意一个开关,最多只能进行一次开关操作。你的任务是,计算有多少种可以达到指定状态的方法。(不计开关操作的顺序)

Input
输入第一行有一个数K,表示以下有K组测试数据。
每组测试数据的格式如下:
第一行 一个数N0 < N < 29)
第二行 N0或者1的数,表示开始时N个开关状态。
第三行 N0或者1的数,表示操作结束后N个开关的状态。
接下来 每行两个数I J,表示如果操作第 I 个开关,第J个开关的状态也会变化。每组数据以 0 0 结束。

Output
如果有可行方法,输出总数,否则输出“Oh,it's impossible~!!” 不包括引号

Sample Input

2
3
0 0 0
1 1 1
1 2
1 3
2 1
2 3
3 1
3 2
0 0
3
0 0 0
1 0 1
1 2
2 1
0 0

Sample Output

4
Oh,it's impossible~!!

Hint
第一组数据的说明:
一共以下四种方法:
操作开关1
操作开关2
操作开关3
操作开关123 (不记顺序) 

高斯消元 和HDU的3364一样是开关问题
http://blog.csdn.net/luricheng/article/details/52506388
这里只是多了个小变形
如果初始状态是010 目标状态是111
那其实就是等价于为初始为000 目标为101
按HDU3364的方法 建立线性方程组 第i个方程的第i个变元系数默认为1 一样的方法即可求解

#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<string>
#include<vector>
#include<deque>
#include<queue>
#include<algorithm>
#include<set>
#include<map>
#include<stack>
#include<time.h>
#include<math.h>
#include<list>
#include<cstring>
#include<fstream>
#include<bitset>
//#include<memory.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pii pair<int,int>
#define INF 1000000007

const int MAXN=33;
int matrix[MAXN][MAXN];

int Gauss(int a[][MAXN],const int&m,const int&n){//m:变元个数 n:方程个数
    int res=0,r=0;//res为自由变元个数 r为增广矩阵的秩
    for(int i=0;i<m;++i){//处理第i个变元
        for(int j=r;j<n;++j)//找到第i个变元系数不为0的方程 并放到第r行
            if(a[j][i]){
                for(int k=i;k<=m;++k)
                    swap(a[j][k],a[r][k]);
                break;
            }
        if(a[r][i]==0){//第i个变元没有系数不为0的 这变元是自由变元
            ++res;
            continue;
        }
        for(int j=0;j<n;++j)//消去其他方程的i变元
            if((j!=r)&&(a[j][i]!=0))
                for(int k=i;k<=m;++k)
                    a[j][k]^=a[r][k];
        ++r;//矩阵的秩+1
    }
    //矩阵的秩下面的方程 系数都为0 0*x1+0*x2+...+0*xm恒等于0 !=0则无解
    for(int i=r;i<n;++i)
        if(a[i][m])//判断是否无解
            return -1;
    return res;
}
int main()
{
    //freopen("/home/lu/文档/r.txt","r",stdin);
    //freopen("/home/lu/文档/w.txt","w",stdout);
    int t,n;
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        for(int i=0;i<MAXN;++i)
            fill(matrix[i],matrix[i]+MAXN,0);
        for(int i=0;i<n;++i)
            scanf("%d",&matrix[i][n]);
        for(int i=0,q;i<n;++i){
            scanf("%d",&q);
            matrix[i][n]^=q;
            matrix[i][i]=1;
        }
        int i,j;
        while(scanf("%d%d",&i,&j),i+j)
            matrix[j-1][i-1]=1;
        int res=Gauss(matrix,n,n);
        if(res==-1)
            cout<<"Oh,it's impossible~!!"<<endl;
        else
            cout<<(1LL<<res)<<endl;
    }
    return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用内容,可以得知这是一道关于迷宫问题的题目,需要使用Java语言进行编写。具体来说,这道题目需要实现一个迷宫的搜索算法,找到从起点到终点的最短路径。可以使用广度优先搜索或者深度优先搜索算法来解决这个问题。 下面是一个使用广度优先搜索算法的Java代码示例: ```java import java.util.*; public class Main { static int[][] maze = new int[5][5]; // 迷宫地图 static int[][] dir = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; // 方向数组 static boolean[][] vis = new boolean[5][5]; // 标记数组 static int[][] pre = new int[5][5]; // 记录路径 public static void main(String[] args) { Scanner sc = new Scanner(System.in); for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { maze[i][j] = sc.nextInt(); } } bfs(0, 0); Stack<Integer> stack = new Stack<>(); int x = 4, y = 4; while (x != 0 || y != 0) { stack.push(x * 5 + y); int t = pre[x][y]; x = t / 5; y = t % 5; } stack.push(0); while (!stack.empty()) { System.out.print(stack.pop() + " "); } } static void bfs(int x, int y) { Queue<Integer> qx = new LinkedList<>(); Queue<Integer> qy = new LinkedList<>(); qx.offer(x); qy.offer(y); vis[x][y] = true; while (!qx.isEmpty()) { int tx = qx.poll(); int ty = qy.poll(); if (tx == 4 && ty == 4) { return; } for (int i = 0; i < 4; i++) { int nx = tx + dir[i][0]; int ny = ty + dir[i][1]; if (nx >= 0 && nx < 5 && ny >= 0 && ny < 5 && maze[nx][ny] == 0 && !vis[nx][ny]) { vis[nx][ny] = true; pre[nx][ny] = tx * 5 + ty; qx.offer(nx); qy.offer(ny); } } } } } ``` 该代码使用了广度优先搜索算法,首先读入迷宫地图,然后从起点开始进行搜索,直到找到终点为止。在搜索的过程中,使用标记数组记录已经访问过的位置,使用路径数组记录路径。最后,使用栈来输出路径。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值