void isvalid(Point p){ Point p1; p1.x = p.x, p1.y = p.y-1; int cnt = 0; if(inbound(p1.x, p1.y) && (forest[p1.x][p1.y] == '.' || forest[p1.x][p1.y]=='*')){ enqueue(p1); count[p1.x][p1.y] = 1+ count[p.x][p.y]; cnt++; } p1.x = p.x-1, p1.y = p.y; if(inbound(p1.x, p1.y) && (forest[p1.x][p1.y] == '.' || forest[p1.x][p1.y]=='*')){ enqueue(p1); count[p1.x][p1.y] = 1+ count[p.x][p.y]; cnt++; } p1.x = p.x, p1.y = p.y+1; if(inbound(p1.x, p1.y) && (forest[p1.x][p1.y] == '.' || forest[p1.x][p1.y]=='*')){ enqueue(p1); count[p1.x][p1.y] = 1+ count[p.x][p.y]; cnt++; } p1.x = p.x+1, p1.y = p.y; if(inbound(p1.x, p1.y) && (forest[p1.x][p1.y] == '.' || forest[p1.x][p1.y]=='*')){ enqueue(p1); count[p1.x][p1.y] = 1+ count[p.x][p.y]; cnt++; } if(cnt > 1){ wand[p.x][p.y] = 1; } }
TileType World::get(const int8_t x, const int8_t y) const { if(inbound(x,y)) return level[(y*width)+x]; else return TileType::Wall; //outside the map is a sea of walls }
std::vector<core::vector3df> findCubesOfInterest(Tline line){ std::vector<core::vector3df> cubes; core::vector3df tracepoint; core::vector3df cubeofinterest; core::vector3df direction = line.end - line.start; direction.normalize(); tracepoint = line.start; if(direction.getLength() != 0){ while(inbound(line.start.X,line.end.X,tracepoint.X) && inbound(line.start.Y,line.end.Y,tracepoint.Y) && inbound(line.start.Z,line.end.Z,tracepoint.Z)){ cubeofinterest.X = abs(floor(tracepoint.X)); cubeofinterest.Y = abs(floor(tracepoint.Y)); cubeofinterest.Z = abs(floor(tracepoint.Z)); if(cubeofinterest.X< 0){ cubeofinterest.X = 0; } if(cubeofinterest.Y<0){ cubeofinterest.Y =0; } if(cubeofinterest.Z<0){ cubeofinterest.Z =0; } if(cubeofinterest.X>= numbox){ cubeofinterest.X = numbox -1; } if(cubeofinterest.Y>= numbox){ cubeofinterest.Y =numbox-1; } if(cubeofinterest.Z>= numbox){ cubeofinterest.Z =numbox-1; } tracepoint = tracepoint + 0.5*direction*Boxsize; if(!cubes.empty()){ if(!cubes.back().equals(cubeofinterest)){ cubes.push_back(cubeofinterest); } } else{ cubes.push_back(cubeofinterest); } } } return cubes; }
/** * @param n an integer * @param m an integer * @param operators an array of point * @return an integer array */ vector<int> numIslands2(int n, int m, vector<Point> &operators) { b.clear(); dj.clear(); this->n = n; this->m = m; b.resize(n * m, false); dj.resize(n * m); int i; for (i = 0; i < n * m; ++i) { dj[i] = i; } int cc = 0; int x, y; int x1, y1; int r, r1; vector<int> ans; vector<int> adj; int j; for (i = 0; i < operators.size(); ++i) { x = operators[i].x; y = operators[i].y; if (b[x * m + y]) { ans.push_back(cc); continue; } b[x * m + y] = true; adj.clear(); for (j = 0; j < 4; ++j) { x1 = x + d[j][0]; y1 = y + d[j][1]; if (inbound(x1, y1) && b[x1 * m + y1]) { adj.push_back(x1 * m + y1); } } if (adj.empty()) { ans.push_back(++cc); continue; } dj[findRoot(adj[0])] = findRoot(x * m + y); for (j = 1; j < adj.size(); ++j) { r = findRoot(x * m + y); r1 = findRoot(adj[j]); if (r != r1) { dj[r1] = r; --cc; } } ans.push_back(cc); } return ans; }
void do_parallax(BITMAP *dest, BITMAP *bumpmap, BITMAP *colormap, int view_x, int view_y) { int x, y; float h, sx, sy; VEC3F vw = vec3f(view_x, view_y, 512.0), p = vec3f(256.0, 256.0, 0.0), v = NORMALIZED_VEC3F(VEC3F_DIFF(vw, p)); float m1 = v.x / v.z, m2 = v.y / v.z; float s = 0.2, k = 0.0, hr; float dx, dy; for(y = 1; y < bumpmap->h - 1; y++) for(x = 1; x < bumpmap->w - 1; x++) { h = (float)PIXEL(bumpmap, x, y); hr = h * s + k; dx = hr * m1; dy = hr * m2; sx = x + dx; sy = y + dy; h = (h + (float)PIXEL(bumpmap, sx, sy)) * 0.5; hr = h * s + k; dx = hr * m1; dy = hr * m2; sx = x + dx; sy = y + dy; h = (h + (float)PIXEL(bumpmap, sx, sy)) * 0.5; hr = h * s + k; dx = hr * m1; dy = hr * m2; sx = x + dx; sy = y + dy; if(inbound(sx, dest->w) && inbound(sy, dest->h)) PIXEL(dest, x, y) = sample_4(colormap, sx, sy, linear_interp); else PIXEL(dest, x, y) = 0; } }
vector<int> spiralOrder(vector<vector<int> > &matrix) { vector<int> vec; if (matrix.empty()) return vec; int m = matrix.size(), n = matrix[0].size(); vector<vector<int> > mark(m, vector<int>(n, 0)); // right, down, left, up const int dir[][2] = {{0, +1}, {+1, 0}, {0, -1}, {-1, 0}}; int r = 0, c = 0; int x = 0; for (int i = 0; i < m*n; ++i) { int val = matrix[r][c]; vec.push_back(val); mark[r][c] = 1; int r2 = r + dir[x][0], c2 = c + dir[x][1]; if (inbound(r2, c2, m, n) && !mark[r2][c2]) { r = r2; c = c2; } else { x = x+1; if (x >= 4) x = 0; r += dir[x][0]; c += dir[x][1]; } } return vec; }
bool do_search(const TMap& map, const TCoords& start_p, const TCoords& finish_p) { static struct { int x, y, d; } dirs[] = { { -1, -1, 14 },{ 0, -1, 10 },{ 1, -1, 14 },{ -1, 0, 10 },{ 1, 0, 10 },{ -1, 1, 14 },{ 0, 1, 10 },{ 1, 1, 14 } }; memset(attrs, 0, sizeof(Attributes) * H * W); while (!opened.empty()) opened.pop(); AttrsPtr current = opened_push(start_p, cost_estimate(start_p, finish_p)); while (!opened.empty()) { current = opened_pop(); if (current.pos.x == finish_p.x && current.pos.y == finish_p.y) return true; for (int i = 0; i < 8; ++i) { int dx = dirs[i].x; int dy = dirs[i].y; TCoords npos; npos.x = current.pos.x + dx; npos.y = current.pos.y + dy; if (!inbound(npos.x, npos.y) || map.isobstacle(npos.x, npos.y)) continue; size_t ni = index2d(npos.x, npos.y); if (attrs[ni].state == st_Closed) continue; TWeight t_gscore = current.pa->gscore + dirs[i].d; if (attrs[ni].state == st_Wild) { opened_push(npos, t_gscore + cost_estimate(npos, finish_p)); } else { if (t_gscore >= attrs[ni].gscore) continue; rearrange(&attrs[ni], t_gscore + cost_estimate(npos, finish_p)); } attrs[ni].ofsx = dx; attrs[ni].ofsy = dy; attrs[ni].gscore = t_gscore; } } return false; }
void DFS(int x, int y, string &s, TrieNode *r, vector<vector<char> > &b) { if (r == NULL) { return; } if (r->isWord) { // A word is found us.insert(s); } int x1, y1; int i; for (i = 0; i < 4; ++i) { x1 = x + d[i][0]; y1 = y + d[i][1]; if (!inbound(x1, y1) || v[x1][y1]) { continue; } v[x1][y1] = true; s.push_back(b[x1][y1]); DFS(x1, y1, s, r->child[b[x1][y1] - 'a'], b); s.pop_back(); v[x1][y1] = false; } }
int main(int argc, char *argv[]) { /* First up: initialize HeavyThing, and like our previous examples, we are not interested in argc/argv from inside our assembler environment: An interesting sidenote here: because HeavyThing has normal function definitions for memcpy, memcmp, etc, those get preferentially linked instead of the libc versions, and so during our C++ initialization, those HeavyThing functions were already called. Fortunately for us in this case, those functions do not require any HeavyThing global state. */ ht$init_args(0, 0); /* We need a default/generic io layer for our top level */ void **iolayer = io$new(); /* Set its virtual method table to our own: */ iolayer[0] = epoll_methods; /* Next in the layers is our ssh server layer, and its argument as 0 specifies to use /etc/ssh host keys */ void *sshserver = ssh$new_server(0); if (!sshserver) { std::cerr << "SSH host key error" << std::endl; exit(1); } /* Unlike our authless version, set our authentication callback */ ssh$set_authcb(sshserver, ssh_authenticate); /* We have to link the iolayer with the ssh layer: */ io$link((void *)iolayer, sshserver); /* And our lowest layer is a real epoll object with defaults */ void *listener = epoll$new((vmethod_t)epoll$default_vtable, 0); /* Link that to our sshserver */ io$link(sshserver, listener); /* Setup an IPv4 socket address for our listener, noting that sockaddr_in_size from epoll.inc is 16 bytes. Listener port == 8001: */ unsigned char addrbuf[16]; inaddr_any(addrbuf, 8001); /* Now we can pass that off to our epoll layer. The HeavyThing's epoll$inbound will return false if bind failure: */ if (!epoll$inbound(addrbuf, sizeof(addrbuf), (void *)iolayer)) { std::cerr << "INADDR_ANY:8001 bind failure." << std::endl; exit(1); } /* Dump a banner to stdout so that we know all is well */ std::cout << "Simple SSH chat server listening on port 8001." << std::endl; /* Pass control (indefinitely) to HeavyThing's epoll layer. */ epoll$run(); /* Not reached. */ }
void World::set(const int8_t x, const int8_t y, const TileType type) { if(inbound(x,y)) level[(y*width)+x] = type; }
int process_ip(pcap_t *dev, const struct ip *ip, struct timeval tv) { char src[16], dst[16], *addr; int incoming; unsigned len; addr = inet_ntoa(ip->ip_src); strncpy(src, addr, 15); src[15] = '\0'; addr = inet_ntoa(ip->ip_dst); strncpy(dst, addr, 15); dst[15] = '\0'; if (is_local_address(ip->ip_src)) incoming = 0; else if (is_local_address(ip->ip_dst)) incoming = 1; else return 1; len = htons(ip->ip_len); switch (ip->ip_p) { struct tcphdr *tcp; uint16_t sport, dport, lport, rport; unsigned datalen; case IPPROTO_TCP: tcp = (struct tcphdr *) ((unsigned char *) ip + sizeof(struct ip)); #if defined(__FAVOR_BSD) sport = ntohs(tcp->th_sport); dport = ntohs(tcp->th_dport); datalen = len - sizeof(struct ip) - tcp->th_off * 4; // 4 bits offset #else sport = ntohs(tcp->source); dport = ntohs(tcp->dest); datalen = len - sizeof(struct ip) - tcp->doff * 4; #endif // Capture only "data" packets, ignore TCP control if (datalen == 0) break; if (incoming) { lport = dport; rport = sport; inbound(tv, ip->ip_dst, ip->ip_src, lport, rport); } else { lport = sport; rport = dport; outbound(tv, ip->ip_src, ip->ip_dst, lport, rport); } break; default: break; } return 0; }