Link: click here ~
I guess this question has been enhanced by data. When I had been struggling for a long time, I handed in several pieces of online code, either WA or TLE. When I was confused, I handed in another copy, AC (although I found a piece of data that he could not pass through with random data ).
I was confident that I could test his code with a random number and use FC to find a different one .. A fatal error was found. In general, BFS or DFS both require a vis array or hash to determine the weight, but this issue is very problematic, for the status of three water cups, the traffic may be 50 in three steps, and the status is marked. The status appears again in five steps (which can be understood as another branch ), however, the traffic is 35. If a two-dimensional array is used to determine the weight (there is no need to use three-dimensional, because it is the same as the two-dimensional method), the traffic in this state will not be updated, the question means to use the smallest traffic to reach this state, whether it is to get the final goal or the status of the nearest target smaller than the target.
Therefore, I feel that the standard solution for this question should not be bfs, because it can be said that it is regarded as brute force. Every time the status (if the traffic is less than the traffic from the previous status) needs to be updated, update until the end. The idea of this question in the ultraviolet () toolkit is dp or dijkstra. I can't think of these two ideas, even if I have created a search task.
Although A is gone, it feels uncomfortable. In addition, the data that I found in the code is: 33 12 113 6.
His conclusion is 174 6, and my conclusion is 171 6, which is also my answer on the WordPress toolkit. However, I think his idea should be correct and I will not elaborate on it .. It's too long.
If you have any ideas, please leave a message.
Code for reference:
#include
#include
#include#include
#include
#include
#include
#include
#include
#include
using namespace std;#define ll long long#define NMAX 20000typedef int state[3];state st[NMAX],a;int pour[NMAX];int record[205];int target;int vis[205][205],vispour[205][205];int try_to_insert(int x,int tpour){ state &k = st[x]; if(vis[k[0]][k[1]] != 1 || vispour[k[0]][k[1]] > tpour) { vis[k[0]][k[1]] = 1; vispour[k[0]][k[1]] = tpour; return 1; } return 0;}int main(){// freopen("input.txt","r",stdin);// freopen("o.txt","w",stdout); int i,j,t,ans; scanf("%d",&t); while(t--) { memset(record,0,sizeof(record)); memset(vis,0,sizeof(vis)); memset(vispour,0,sizeof(vis)); scanf("%d%d%d%d",&a[0],&a[1],&a[2],&target); st[1][0] = st[1][1] = 0; st[1][2] = a[2]; memset(pour,0,sizeof(pour)); record[a[2]] = 1; int front = 1,rear = 2; vis[0][0] = 1; bool flag = false; while(front < rear) { for(i = 0; i < 3; i++) if(record[st[front][i]] == 0 || pour[record[st[front][i]]] > pour[front]) record[st[front][i]] = front; for(i = 0; i < 3; i++) if(st[front][i] == target) { if(flag) ans = pour[ans] > pour[front]?front:ans; else ans = front; flag = true; break; } for(i = 0; i < 3; i++) { state &w = st[front]; if(w[i] != 0) { for(j = 0; j < 3; j++) { if(i == j || (i != j && w[j] == a[j])) continue; state &temp = st[rear]; memcpy(temp,w,sizeof(w)); int pp; if(w[i] + w[j] > a[j]) { pp = a[j] - w[j]; temp[i] = w[i] - pp; temp[j] = a[j]; } else { pp = w[i]; temp[i] = 0; temp[j] = w[j] + pp; } int tpour; tpour = pour[front] + pp; if(try_to_insert(rear,tpour)) { pour[rear] = tpour; rear++; } } } } front++; } if(record[target] == 0) { for(i = target; record[i] == 0; i--); printf("%d %d\n",pour[record[i]],i); } else printf("%d %d\n",pour[ans],target); } return 0;}