/*Robot(计算最短距离,用Dijkstra算法) 建立邻接表,使用优先队列按照距离入队 */#include <cstdio>#include <cstring>#include <queue>#include <vector>using namespace std;#define N 10005#define INF 1000000000int map[105][105], row, col;int dx[] = {-1, 1, 0, 0};int dy[] = {0, 0, -1, 1};bool vis[N];struct Node{ int idx, w; Node(){} Node(int id, int ww){idx = id, w = ww;} bool operator<(const Node& b) const { return w > b.w; }};struct Edge{ int v, w; Edge(){} Edge(int vv, int ww){v = vv, w = ww;}};vector<Edge> g[N];bool isvalid(int x, int y){ return x >= 0 && x < row && y >= 0 && y < col;}inline int getNumber(int x, int y){ return x*col + y;}int dijkstra(int s, int t){ int n = row*col; memset(vis, false, sizeof(vis)); priority_queue<Node> pq; //优先队列,插入时,会重载Node中的<操作符,具有较小能耗的w排在前面 pq.push(Node(s, map[s/col][s%col])); for(int i=1; i<=n; i++) { Node tmp = pq.top();pq.pop(); while(vis[tmp.idx]) { tmp = pq.top();pq.pop(); } vis[tmp.idx] = true; if(tmp.idx == t) return tmp.w; //使用广度优先搜索 for(unsigned j=0; j<g[tmp.idx].size(); j++) { int v = g[tmp.idx][j].v, w = g[tmp.idx][j].w; if(!vis[v]) pq.push(Node(v, tmp.w+ w)); } } return -1;}int main(){ int T, u, v, x, y; scanf("%d", &T); while(T--) { scanf("%d %d", &row, &col); for(int i=0; i<row*col; i++) g[i].clear(); for(int i=0; i<row; i++) for(int j=0; j<col; j++) scanf("%d", &map[i][j]); for(int i=0; i<row; i++) { for(int j=0; j<col; j++) { u = getNumber(i, j); //把u附近的4个方向的格子放进其邻接表中 for(int d = 0; d < 4; d++) { x = i + dx[d], y = j + dy[d]; if(isvalid(x, y)) { v = getNumber(x, y); g[u].push_back(Edge(v, map[x][y])); } } } } scanf("%d %d %d %d", &u, &v, &x, &y); u--, v--, x--, y--; u = getNumber(u, v), v = getNumber(x, y); printf("%d\n", dijkstra(u, v)); } return 0;}