I used to write programs in c ++ before, so it is generally easier to think about programs in the direction of data structures. However, it usually takes a lot of time to design your own data structures. Yesterday I checked python and found that the scripting language is really it is really good to use it for simulation operations. You can first think of a solution to the problem. Use python to test it first. If it is feasible, it is a good choice to use c ++ to rewrite it for running efficiency.
I saw this question when I was reading the operating system yesterday.
This is a typical topic in the operating system for allocating resources to multiple processes and finally achieving security. It can be understood as finding a secure path.
If you use c ++ to write, you should use the deep-Priority Search Algorithm and then slowly think about the solution. to practice the python just learned, I use python numpy to solve this problem.
Because I have not yet learned how to write functions, I have written four loops, but the algorithm complexity should be similar. I want to optimize it after learning for a while.
import numpya=numpy.array(([1,1,2,4], [0,1,2,2], [1,1,1,0], [1,1,1,1]))b=numpy.array(([1,2,3,6], [1,1,2,2], [1,2,1,1], [1,1,2,3]))c=numpy.array([0,1,0,1])d=b-afor i in range(0,4):j = 0for j in range(0,4):if d[i][j]>c[j]:break#print 'e'#if j==3 and d[i][j]<=c[j]:else:e=numpy.array([0,0,0,0])f=ce[i]=1f=f+d[i]d[i]=numpy.array((0,0,0,0))for k in range(0,4):if e[k]==1:continuen = 0for n in range(0,4):if d[k][n]>f[j]:break;#if n==3 and d[k][n]<=f[j]:else:e[k]=1f=f+d[k]d[k]=numpy.array((0,0,0,0))for m in range(0,4):if e[m]==1:continuep = 0for p in range(0,4):if d[m][p]>f[p]:break#print p#print m#if p==3 and d[m][p]<=f[p]:else:e[m]=1f=f+d[m]d[m]=numpy.array((0,0,0,0))for q in range(0,4):if e[q]==1:continuer = 0for r in range(0,4):if d[q][r]>f[r]:break#if r==3 and d[q][r]<=f[r]:else:print i+1print k+1print m+1print q+1#print 'heihei'exit()d[m]=b[m]-a[m]e[m]=0d[k]=b[k]-a[k]e[k]=0d[i]=b[i]-a[i]e[i]=0