刷题记录(NC15665 maze,NC50243 小木棍)

NC15665 maze

题目链接

关键点:

1、易错点:一个点可能有很多的传送门,因此我们用vector来讲该坐标和该坐标的传送门一一对应

struct xb
{
    int x, y;
}st, ed;

vector<xb>v[N][N];

2、关于传送门,其出口可能为陷阱

3、然后这题与一般的走迷宫的区别在于多了个传送门,因此我们用优先队列,对于传送门的步数,就为传送门入口的步数+3,然后该队列按照步数从小到大排序,并且用vis数组来看该点是否有到过,这样就可以避免同一个点不同的步数

完整代码:

# include <bits/stdc++.h>
using namespace std;
const int N = 3e2+5;
struct xb
{
	int x, y;
}st, ed;
struct Q{
	int x, y, step;
	bool operator < (const Q &s) const{
		return step>s.step;
	}
};
vector<xb>v[N][N];
char s[N][N];
bool vis[N][N];
priority_queue<Q>q;
int n, m, t;
int X[4] = {0, 0, 1, -1};
int Y[4] = {1, -1, 0, 0};
int bfs()
{
	while (!q.empty())
	{
		auto T = q.top(); q.pop();
		int x = T.x, y = T.y, st = T.step;
		if (vis[x][y] || x<1||x>n||y<1||y>m||s[x][y] == '#') continue;
		if (x == ed.x && y == ed.y)
		return st;
		vis[x][y] = true;
		for (int i=0; i<4; i++) q.push({x+X[i], y+Y[i], st+1});
		for (auto u:v[x][y]) q.push({u.x, u.y, st+3});
	}
	return -1;
}
int main()
{
	while (cin>>n>>m>>t)
	{
		while (!q.empty())
		q.pop();
		for (int i=1; i<=n; i++)
		{
			for (int j=1; j<=m; j++)
			{
				cin>>s[i][j];
				vis[i][j] = false;
				v[i][j].clear();
				if (s[i][j] == 'S')
				st = {i, j};
				if (s[i][j] == 'T')
				ed = {i, j};
			}
		}
		q.push({st.x, st.y, 0});
		for (int i=1; i<=t; i++)
		{
			int x1, x2, y1, y2;
			cin>>x1>>y1>>x2>>y2;
			x1++, y1++, x2++, y2++;
			v[x1][y1].push_back({x2, y2});
		}
		for (auto u:v[st.x][st.y]) q.push({u.x, u.y, 3});
		cout<<bfs()<<endl;
	}
	
	
	return 0;
}

NC50243 小木棍

题目链接

关键点:

1、首先明确枚举原先的小木棍长度,题目要求求最短的原先木棍长度,我们从可能最小的木棍长度从小到大枚举,遇到可行的就输出

2、将木棍长度排序,然后求和,从第一个数组开始枚举到所有的和

3、剪枝:(一)我们从大木棍开始排,(二)遇到拿过的木棍就跳过,(三)遇到当前木棍大于剩余长度跳过,(四)遇到当前木棍拼不上,且当前木棍为最后一个(一个长木棍)或者第一个(一个长木棍)时,那么后面的木棍肯定都不行,直接返回false,(五)然后对于当前木棍拼不上的,那么后面和他相同长度的也可以直接跳过

4、还有一步就是在枚举长木棍长度时,对于长度i,要保证

sum%i==0

这样才有可能由小木棍拼成

# include <bits/stdc++.h>
using namespace std;
int n, minn = 100, sum;
int a[100];
int vis[100];
bool dfs(int num, int len, int rest, int now)
{
    if (num==0&&rest==0) return true;
    if (rest==0) rest = len, now = 1;
    for (int i=now; i<=n; i++)
    {
        if (vis[i]) continue;
        if (a[i]>rest) continue;
        vis[i] = 1;
        if (dfs(num-1, len, rest-a[i], i)) return true;
        vis[i] = 0;
        if (a[i] == rest || len==rest) break;
        while (a[i]==a[i+1])
            i++;
    }
    return false;
}
bool cmp(int x, int y)
{
    return x>y;
}
int main()
{
    cin>>n;
    for (int i=1; i<=n; i++)
    {
        cin>>a[i];
        sum += a[i];
    }
    sort(a+1, a+1+n, cmp);
    for (int i=a[1]; i<=sum; i++)
    {
        memset(vis, 0, sizeof(vis));
        if (sum%i!=0) continue;
        if (dfs(n, i, i, 1))
        {
            cout<<i<<endl;
            break;
        }
    }
    
    
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值