poj 3487 稳定婚姻问题

poj 3487

详细解释请点击

//#include <bits/stdc++.h>
#include<stdio.h>
#include<string.h>
#include<string>
#include<math.h>
#include<algorithm>
#include<iostream>
#include<queue>
#include<vector>
#include<stack>
#include<map>
#include<set>
#include<stdlib.h>
#include<time.h>
#include <iomanip>
#define lowbit(x) (x&(-x))
#define inf  0x7fffffff
#define linf 0x7fffffffffffffff
#define mem(x,y) memset(x,y,sizeof(x))
#define fup(i,x,y) for(int i=(x);i<=(y);i++)
#define fdn(i,x,y) for(int i=(x);i>=(y);i--)
#define sp(x) setprecision(x)
#define sd(n) scanf("%d",&n)
#define sdd(n,m) scanf("%d%d",&n,&m)
#define sddd(n,m,k) scanf("%d%d%d",&n,&m,&k)
#define sld(n) scanf("%lld",&n)
#define sldd(n,m) scanf("%lld%lld",&n,&m)
#define slddd(n,m,k) scanf("%lld%lld%lld",&n,&m,&k)
#define sf(n) scanf("%lf",&n)
#define sff(n,m) scanf("%lf%lf",&n,&m)
#define sfff(n,m,k) scanf("%lf%lf%lf",&n,&m,&k)
#define sc(n) scanf("%s",n)
#define lowbit(x) (x&(-x))
#define pf(x) printf("%d\n",x)
#define pfl(x) printf("%lld\n",x)
#define pff(x) printf("%lf\n",x)
#define debug printf("!!\n");
#define N 1000005
#define M 4000009
#define pi acos(-1)
#define eps 1e-2
//cout.setf(ios::fixed);
//freopen("out.txt","w",stdout);// freopen("in.txt","r",stdin);
using namespace std;
typedef long long  ll;
typedef double db;
using namespace std;
typedef long long ll;
using namespace std;
int man[35][35],women[35][35];
int manperfer[35],num[35],womennow[35];
stack<int> s;
map<char,int> ma,wo;
char c1[35],c2[35];
int n;
void ini()
{
    mem(manperfer,0);
    mem(womennow,0);
}
int fi(int x,int y)
{
    fup(i,1,n)
    if(women[x][i]==y) return i;
    return 0;
}
void choose(int x)
{
    int prefer=man[x][num[x]];
    if(womennow[prefer]==0)
    {
        womennow[prefer]=x;
        manperfer[x]=prefer;
        return ;
    }
    int old=fi(prefer,womennow[prefer]);
    int now=fi(prefer,x);
    if(old<now)
    {
        num[x]++;
        s.push(x);
    }
    else
    {
        int tp=womennow[prefer];
        num[tp]++;
        s.push(tp);
        womennow[prefer]=x;
        manperfer[x]=prefer;
    }
}
int main()
{
    int t;
    sd(t);
    while(t--)
    {
        ini();
        sd(n);
        fup(i,1,n)
            sc(c1+i),num[i]=1;
        sort(c1+1,c1+1+n);
        fup(i,1,n)
            ma[c1[i]]=i;

        fup(i,1,n)
            sc(c2+i);
        sort(c2+1,c2+1+n);
        fup(i,1,n)
            wo[c2[i]]=i;
        char like[35];
        fup(j,1,n)
        {
            sc(like);
            fup(i,2,n+1)
            {
                char x;
                x=like[i];
                man[ma[like[0]]][i-1]=wo[x];

            }
        }
        fup(j,1,n)
        {
            sc(like);
            fup(i,2,n+1)
            {
                char x;
                x=like[i];
                women[wo[like[0]]][i-1]=ma[x];
            }
        }
        fup(i,1,n)
            choose(i);
        while(!s.empty())
        {
            int tp=s.top();
            s.pop();
            choose(tp);
        }
        fup(i,1,n)
        printf("%c %c\n",c1[i],c2[manperfer[ma[c1[i]]]]);
        puts("");
    }
    return 0;
}
  • 0
    点赞
  • 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、付费专栏及课程。

余额充值