Gumdrop 发布的文章

质点动力学的一道习题2.15

题目来自大学物理(少学时),高等教育出版社,第65页。

题目

在光滑水平桌面上,平放一个固定的半圆形屏障,质量为$m$的滑块以初速$v_0$沿切线方向进入屏障内,如题图2.15所示,设滑块与屏障间的摩擦因数为$\mu$,求证:当滑块从屏障另一端滑出时,摩擦力所做的功为:

$$ A=\frac{1}{2}mv_0^2(e^{-2\pi\mu}-1) $$ ![题图2.15.jpg][1] ## 故事

以滑块运动方向为正方向。

开始以为有重力作用,满足:

$$ ma_\tau=P\sin\theta+F_f=m\frac{\text{d}v}{\text{d}r}=m\frac{\text{d}v}{\text{d}x}\frac{\text{d}x}{\text{d}t}=mv\frac{\text{d}v}{\text{d}x} \\ \ \\ ma_n=P\cos\theta+N=m\frac{v^2}{r} \\ \ \\ \text{d}A=F_f\,\text{d}x=F_fr\,\text{d}\theta \\ $$

然后做不了一点,拿$\theta$没有办法。

$$ \frac{\text{d}v}{P\sin\theta-\frac{\mu}{r}v^2+\mu P\cos\theta}=\text{d}t \\ \ \\ \frac{v\ \text{d}v}{P\sin\theta-\frac{\mu}{r}v^2+\mu P\cos\theta}=\text{d}x $$

一看题干发现

……,平放一个固定的半圆形屏障,……

没重力了……

$$ \begin{cases} F_f=ma_\tau=m\frac{\text{d}v}{\text{d}t}=m\frac{\text{d}v}{\text{d}x}\frac{\text{d}x}{\text{d}t}=-\mu N \\ \\ N=ma_n=m\frac{v^2}{r} \end{cases} \\ \ \\ \begin{align*} F_f=-\mu m\frac{v^2}{r}=mv\frac{\text{d}v}{\text{d}x} & \Rightarrow & -\mu m\frac{v^2}{r}\ \text{d}x=mv\ \text{d}v \\ & \Rightarrow & -\frac{\mu}{r}\ \text{d}x=\frac{\text{d}v}{v} \\ & \Rightarrow & \int_0^x-\frac{\mu}{r}\ \text{d}x=\int_{v_0}^v\frac{\text{d}v}{v} \\ & \Rightarrow & v=v_0e^{-\frac{\mu}{r}x} \end{align*} $$

于是可以写出$F_f(x)$,可以进行积分$\int_0^{\pi r}F_f\ \text{d}x$。

$$ A=\int_0^{\pi r}-\mu m\frac{v^2}{r}\ \text{d}x=\frac{1}{2}mv_0^2(e^{-2\pi\mu}-1) $$

如果不使用$F_f=m\frac{\text{d}v}{\text{d}x}\frac{\text{d}x}{\text{d}t}$而使用$m\frac{\text{d}v}{\text{d}t}$,则得到:

$$ v(t)=\frac{v_0r}{\mu v_0t+r} $$

用不了一点。

总结

  • 认真审题,细心答卷。

  • 把“从屏障另一端滑出”翻译为从状态$(t=0,v=v_0,x=0)$到$(t=?,v=?,x=\pi r)$,立刻可知不要先尝试对$t$和$v$积分。

  • tamatamatama

<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>

在与联盟的战斗中屡战屡败后,帝国撤退到了最后一个据点。依靠其强大的防御系统,帝国击退了联盟的六波猛烈进攻。 经过几天的苦思冥想,联盟将军亚瑟终于注意到帝国防御系统唯一的弱点就是能源供应。
该系统由 N 个核电站供应能源,其中任何一个被摧毁都会使防御系统失效。
将军派出了 N 个特工进入据点之中,打算对能源站展开一次突袭。不幸的是,由于受到了帝国空军的袭击,他们未能降落在预期位置。
作为一名经验丰富的将军,亚瑟很快意识到他需要重新安排突袭计划。
他现在最想知道的事情就是哪个特工距离其中任意一个发电站的距离最短。
你能帮他算出来这最短的距离是多少吗?

输入格式:

输入中包含多组测试用例。
第一行输入整数 T,代表测试用例的数量。
对于每个测试用例,第一行输入整数 N(1≤N≤100000 )。
接下来 N 行,每行输入两个整数 X(0≤X) 和 Y(Y≤1000000000),代表每个核电站的位置的 X,Y 坐标。
再接下来 N 行,每行输入两个整数 X 和 Y,代表每名特工的位置的 X,Y 坐标。

输出格式:

每个测试用例,输出一个最短距离值,结果保留三位小数。
每个输出结果占一行。

输入样例:

在这里给出一组输入。例如:

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

输出样例:

在这里给出相应的输出。例如:

1.414
0.000
### 源代码
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
const double INF=1e10;
const int N=100005;
 
int n;
struct node{
	double x,y;
	int flag;
	bool operator < (const node &a) const{
		return x<a.x;
	} 
}opt[N<<1],temp[N<<1];//在一段实现功能的程序中,如果要使用temp,就要先对要使用的部分进行赋值

double dis(node a,node b){//返回两点之间的距离 
	if(a.flag==b.flag) return INF;
	return sqrt(pow(abs(a.x-b.x),2)+pow(abs(a.y-b.y),2));
}

double solve(int l,int r){// 返回排序后下标l到r这个区间中最小点距
	//如果下标区间只有这一个点 返回最大值  
	if(l>=r) return INF;
	
	//二分递归,求两个左右区间的最小点距,并且将小的赋给ans存储 
	int mid=(l+r)>>1;
	double mid_x=opt[mid].x;
	double ans=min(solve(l,mid),solve(mid+1,r));
	
	//二分排序的合并过程  
	int left=l,right=mid+1,t=l;
	while(left<=mid && right<=r){//下标区间内所有点存于temp的相同下标区间
		if(opt[left].y<=opt[right].y) temp[t++]=opt[left++];
		else temp[t++]=opt[right++];
	}//先执行只有两个点的排序,成功比较大小;后面更多点的排序建立在其基础上:两个已排序的数列插值合并还是已排序的。
	while(left<=mid) temp[t++]=opt[left++];//这两个循环只有一个能进行
	while(right<=r) temp[t++]=opt[right++];
	for(int i=l;i<=r;i++){
		opt[i]=temp[i];
	}
	
	//从左到右将opt中的在mid_x-ans至mid_x+ans的点存进temp
	//这样temp中的点既是按y排序的,又是在mid_x-ans至mid_x+ans区间里的 
	int k=0;
	for(int i=l;i<=r;i++){
		if(opt[i].x>=mid_x-ans&&opt[i].x<=mid_x+ans){
			temp[k++]=opt[i];
		}
	}
	
	//从有序的temp数组中 找到每个点y坐标上面不超过ans的点的点距离求出
	//并且更新答案  
	for(int i=0;i<k;i++){
		for(int j=i-1;j>=0&&temp[i].y-temp[j].y<=ans;j--){
			ans=min(ans,dis(temp[i],temp[j]));
		}
	}
	
	//返回这个l到r下标的最小点距 
	return ans;
}

int main()
{
	int t;
	cin>>t;
	while(t--){
		cin>>n;
		for(int i=0;i<n;i++) {
			cin>>opt[i].x>>opt[i].y;
			opt[i].flag=0;
		}
		for(int i=n;i<n<<1;i++) {
			cin>>opt[i].x>>opt[i].y;
			opt[i].flag=1;
		}
		sort(opt,opt+(n<<1));
		printf("%.3f\n",solve(0,(n<<1)-1));
	}
	return 0;
} 

### 题解

分治、穷举、贪心?

由于题目相比普通的最近点对问题,要求出两点集间的最短距离,为避免分类可以假设不同点集的点是一个点集内距离为INF的点。距离函数dis()。

先把点按x坐标排序。在某下标区间[l,r]的solve函数也实现了该下标区间内的点转换成按y排序再储存到原处。

区间按下标区间中点mid分成两块,最近距离为\(\min\{d_左,d_右,d_{左右}\}\)。

求两边点间最短距离的贪心算法,可以只取mid点x坐标旁边的部分点满足\(mid.x-ans \leq x \leq mid.x+ans,\qquad ans=\min\{d_左,d_右\}\)。

用一般方法求出这部分点间的最小距离,就完成该步的操作,返回该下标区间内的最近距离。

递归solve得到答案。

<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script><style type="text/css">em{font-size: large;font-weight: bold;}</style>

洛谷P2324 [SCOI2005] 骑士精神

题目在P2324 [SCOI2005] 骑士精神

初步分析

输入棋盘当前的状态,要求出从当前状态转移到目标状态所需要的最少变换次数。将每种状态作为无向图的一个节点,能通过走“马”字相互转换的两种状态对应图上的两个相邻节点。

此时问题就转变成求从某结点A到目标节点B的最短路径的长度。

棋盘的状态不同,即棋盘上黑白子的位置、空白的位置不同;棋盘不同状态的转换通过能移动到空白处的棋子移动(改变位置)完成。

算法选用

假设大师在这题会用IDA*。
  • 棋盘的状态非常多,而题目有15的路径长度限制,饱学之士将能使用启发式搜索进行剪枝。对于图中的任意点M,经过该点的路径长\(l=AM+MB \leq 15\)。从始点开始绘制路径到\(M\)时\(MA\)已经有值,则在搜索树上剪去\(MB>15-AM\)的结点。

    可以不严谨地证明当棋盘在M状态下有n个位置的状态不同时,最少需要n-1步来达到相同,计算两个状态间不同的个数为\(n = h(M,B)\) ,则在搜索树上剪去\(n-1+AM \geq 15\)的结点。这是启发式搜索的估值函数

  • 常用求最短路径的广度优先搜索由于不好储存节点状态(可能超内存)而不能实行,深度优先搜索由于连续两次搜索的状态相邻可以避免这一问题。为避免深度过大超时需要最大深度常量,在递归函数中能访问,与当前深度做判断是否剪枝

    一般使用此方法会让深度递增到最大值,每个深度都重新搜索,叫限制深度的(迭代加深)深度优先搜索

代码实现

  1. 创建一些全局量
  2. 这题的终点是个常量。走“马”也很常规。

    //最后目标
    int goal[5][5] = {
    {1,1,1,1,1},
    {0,1,1,1,1},
    {0,0,2,1,1},
    {0,0,0,0,1},
    {0,0,0,0,0}
    };
    //用于走马
    int dx[8] = { 1,1,2,2,-1,-1,-2,-2 };
    //用于走马
    int dy[8] = { 2,-2,1,-1,2,-2,1,-1 };

  3. 输入储存数据
  4. 没有什么特别,注意输入数据的字符中间没有空格。

    char c;
    for (int i = 0; i < 5; ++i)
    {
    for (int j = 0; j < 5; ++j)
    {
    cin >> c;
    if (c == '*') {
    blank[0] = i;
    blank[1] = j;
    chessboard[i][j] = 2;
    }
    else chessboard[i][j] = c - '0';
    }
    }//输入了棋盘

  5. “ID”部分
  6. 终止条件是深度depth、估值函数eval满足\(depth>maxdepth\)、\(eval+depth-1>maxdepth\)回溯;或\(eval=0\)跳出递归,将depth赋给能在main()中被访问的变量。

    这里选择当前节点的空白处位置x和y、棋子位置chessboard(放在了全局)、当前深度depth和本轮的最大深度maxdepth作为递归dfs()的参数。

    //储存棋盘
    int chessboard[5][5];
    //标记是否成功了
    bool succ = false;
    //限制深度的深搜
    void dfs(int depth,int x,int y, int maxdepth) {
    if (depth > maxdepth) {
    return;
    }//步数超过了限制的最大步数
    int count = eval();
    if (count == 0) {
    succ = true;
    return;
    }//达成目标
    if (depth + count - 1 > maxdepth)
    {
    return;
    }//未来不能满足条件了

    for (int i = 0; i lt; 8; ++i) {
    int a = x + dx[i], b = y + dy[i];
    if (0 <= a && a < 5 && 0 <= b && b < 5) {
    swap(chessboard[a][b], chessboard[x][y]);
    dfs(depth + 1, a, b, maxdepth);
    if (succ)return;
    swap(chessboard[a][b], chessboard[x][y]);//回溯
    }
    }
    }
    int main(){
    ...
    int ans = -1;
    for (int i = 1; i <= 15; ++i) {
    dfs(0, x, y, i);
    if (succ) {
    ans = i;
    break;
    }
    }
    cout << ans << endl;
    ...
    }

  7. 估值函数
  8. 根据前面的分析,估值函数只需要统计两个状态的不同处的个数并返回就可以。

    //估值函数
    int eval() {
    int ans = 0;
    for (int i = 0; i < 5; ++i) {
    for (int j = 0; j < 5; ++j) {
    if (chessboard[i][j] != goal[i][j]) {
    ans++;
    }
    }
    }
    return ans;
    }

最终代码

#include<iostream>

using namespace std;
//储存棋盘
int chessboard[5][5];
//最后目标
int goal[5][5] = {
{1,1,1,1,1},
{0,1,1,1,1},
{0,0,2,1,1},
{0,0,0,0,1},
{0,0,0,0,0}
};
//用于走马
int dx[8] = { 1,1,2,2,-1,-1,-2,-2 };
//用于走马
int dy[8] = { 2,-2,1,-1,2,-2,1,-1 };
//空白坐标
int blank[2];
//标记是否成功了
bool succ = false;

//估值函数
int eval() {
int ans = 0;
for (int i = 0; i < 5; ++i) {
for (int j = 0; j < 5; ++j) {
if (chessboard[i][j] != goal[i][j]) {
ans++;
}
}
}
return ans;
}

//限制深度的深搜
void dfs(int depth,int x,int y, int maxdepth) {
//if (maxdepth == 7)cout << depth << endl;
if (depth > maxdepth) {
return;
}//步数超过了限制的最大步数
int count = eval();
if (count == 0) {
succ = true;
return;
}//达成目标
if (depth + count - 1 > maxdepth)
{
return;
}//未来不能满足条件了

for (int i = 0; i < 8; ++i) {
int a = x + dx[i], b = y + dy[i];
if (0 <= a && a < 5 && 0 <= b && b < 5) {
swap(chessboard[a][b], chessboard[x][y]);
dfs(depth + 1, a, b, maxdepth);
if (succ)return;
swap(chessboard[a][b], chessboard[x][y]);//回溯
}
}
}

int main() {
int t; cin >> t;
while (t--)
{
succ = false;
char c;
for (int i = 0; i < 5; ++i)
{
for (int j = 0; j < 5; ++j)
{
cin >> c;
if (c == '*') {
blank[0] = i;
blank[1] = j;
chessboard[i][j] = 2;
}
else chessboard[i][j] = c - '0';
}
}//输入了棋盘

int ans = -1;
for (int i = 1; i <= 15; ++i) {
dfs(0, blank[0], blank[1], i);
if (succ) {
ans = i;
break;
}
}
cout << ans << endl;
}
}

附加内容

对于没见过迭代加深DFS的菜的人,可以通过构造一个小的数据结构来储存状态,避免超出内存。使用广度优先搜索和A*。

广度优先搜索中,对每个结点的搜索(直接写在了main()中)选用描述当前棋盘状态的board、当前深度depth作为隐式的参数,最大深度15可以看作常量参数。由于难以回溯而采取用队列queue<pack>储存信息的方式。

添加该状态的前一个状态的空白处坐标在pack结构内,可以减去重复枝;应该也可以用unordered_map来储存搜索过的所有状态来减少重复。代码使用了priority_queue来提速。

#include<iostream>
#include<bitset>
#include<queue>
#include<chrono>

using namespace std;
typedef bitset<25> bs;

//用于走马
int dx[8] = { 1,1,2,2,-1,-1,-2,-2 };
//用于走马
int dy[8] = { 2,-2,1,-1,2,-2,1,-1 };

//记录棋盘状态
struct chessboard {
bs pieces;
int blank;
//空白位置的坐标
const pair cdntBlank() const {
return make_pair(blank / 5, blank % 5);
}
//将(x,y)转换成pos
static int castPos(const pair& p) {
if (0 <= p.first && p.first < 5 && 0 <= p.second && p.second < 5) {
return p.first * 5 + p.second;
}
else return -1;
}
};
const chessboard goal = { 549855 ,12 };

//返回移动后的棋盘状态
chessboard moveReturn(const chessboard& c, int pos) {
bs temp = c.pieces;
temp[c.blank] = c.pieces[pos];
temp[pos] = 0;
return chessboard{ move(temp) ,pos };
}

//估值函数
int evaluateDistance(const chessboard& crt_board, const chessboard& goal) {
int ans = 0;
for (int i = 0; i < 25; ++i) {
ans = (crt_board.pieces ^ goal.pieces).count();
}
if (!crt_board.pieces[goal.blank])ans++;
if (!goal.pieces[crt_board.blank])ans++;
if (goal.blank == crt_board.blank)ans -= 2;
return ans;
}

//用于队列的类
struct pack
{
chessboard board;
int last_blank;
int depth;
int eval;
class less {
public:
int operator()(const pack& a, const pack& b) {
return a.depth + a.eval > b.depth + b.eval;
}
};
};

//queue bfs_queue;

int main() {
int t; cin >> t;
const int MAXDEPTH = 15;
while (t--)
{
chessboard board{ 0,0 };
bs temppieces;
for (int i = 0; i < 25; ++i) {
static char c;
cin >> c;
if (c == '1')temppieces[i] = 1;
else if (c == '*')board.blank = i;
//else temppieces[i] = 0;
}
board.pieces = move(temppieces);//输入棋盘

//bfs_queue = queue();
int ans = -1;
priority_queue,pack::less> bfs_queue;
bfs_queue.push({ board,-1,0,evaluateDistance(board,goal) });
int pos;
do
{
pack front = bfs_queue.top();
bfs_queue.pop();

if (front.eval == 0) {
ans = front.depth;
break;
}
else if (front.depth >= MAXDEPTH) {
continue;
}
if (front.depth + front.eval - 1 > MAXDEPTH) {
continue;
}

auto x_y = front.board.cdntBlank();
int cx_y = chessboard::castPos(x_y);
for (int i = 0; i < 8; ++i) {
pos = chessboard::castPos(make_pair(x_y.first + dx[i], x_y.second + dy[i]));
if (pos == front.last_blank)continue;
if (pos != -1) {
chessboard newboard = move(moveReturn(front.board, pos));
bfs_queue.push({ newboard,
cx_y,
front.depth + 1,
evaluateDistance(newboard,goal) });
}
}
} while (!bfs_queue.empty());
cout << ans << endl;
}
}